We use it when we need to develop a code that deals with only the parent class. Downcasting is used when we need to develop a code that accesses behaviors of the child class.
JavaTpoint offers too many high quality services. Mail us on [email protected] , to get more information about given services. Please mail your requirement at [email protected] Duration: 1 week to 2 week. Java Training Java Tutorial. Abstract class Interface Abstract vs Interface. Package Access Modifiers Encapsulation. PrintData ; obj2. Next Topic What is Polymorphism in Java.
Reinforcement Learning. R Programming. React Native. Python Design Patterns. Python Pillow. Python Turtle. Upcasting and Downcasting Java support Run-time polymorphism with the help of method overriding. Method overriding only applicable when the signature prototype, return type, name, argument of the superclass is the same as the subclass method, then it is known as method overriding. When the base class reference referred to the object of the child class is the concept of upcasting in Java.
Downcasting: Similarly, downcasting means the typecasting of a parent object to a child object. Downcasting cannot be implicit. The following image illustrates the concept of upcasting and downcasting: Attention reader! Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. Example: Let there be a parent class. There can be many children of a parent. The child inherits the properties of the parent.
Therefore, the child can be implicitly upcasted to the parent. However, we can forcefully cast a parent to a child which is known as downcasting.
After we define this type of casting explicitly, the compiler checks in the background if this type of casting is possible or not. One thing to note about the base class is the fact that I have used protected variables for the X and Y co-ordinates. Here are our two derived classes. And finally Circle Notice, the highlighted code blocks. We have overridden the base class implementation of Draw in each of our derived classes.
Basically override tells the compiler that we are intentionally overriding the behaviour of Draw. We will now test the classes we have written, by putting together a simple piece of code that will create an instance of Shape, Circle and Square and call the Draw methods for each type. Draw ;. So what's happened that's polymorphic?
Well, at this point absolutely nothing! Consider the code below. What we have done is to create an array of the type Shape. Because Square and Circle are derived from Shape, we are able to put them in our array. What we are then doing is looping through all the elements of our array and calling Draw for each of our types. Because we have overridden the Draw method in each of our derived classes the output of our code is:. If we did not override Draw in one of our derived classes, the base class implementation of Draw would be called.
For example, if we declared Draw in our Circle class as follows:. Our output would be:. By declaring the method as shown above, we will receive a compiler warning as follows:. Draw : Hides inherited member. Draw :To make the current member override that implementation, add the override keyword.
Otherwise add the new keyword. If we do not want to override the base class method, we need to use the new keyword when declaring our method, for example:. This will remove the compiler warning. Basically we are telling the compiler that we are not overriding the base class implementation. So what have we achieved with polymorphism? What we have been able to do is create an array of shapes and add a specific shape to each element of the array.
When drawing each shape, we have not concerned ourselves with the fact that the shape could be a circle or a square. We have simply said, "here is an array of shapes, please draw them! Now we come on to one of the original goals of the article. With the simple example above of polymorphism, you should be able to quickly understand what up-casting is, in fact we have already used up-casting in our example. The diagram below is a UML diagram for our polymorphism example.
Consider the following code:. We have cast Circle to the type Shape. This is perfectly legal code as we saw in the Polymorphism example.
0コメント