Upcasting in Java with Examples
Inheritance is an important pillar of OOP(Object Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features (fields and methods) of another class. There are two ways in which the objects can be initialized while inheriting the properties of the parent and child classes. They are:
- Child c = new Child(): The use of this initialization is to access all the members present in both parent and child classes, as we are inheriting the properties.
- Parent p = new Child(): This type of initialization is used to access only the members present in the parent class and the methods which are overridden in the child class. This is because the parent class is upcasted to the child class.
What is upcasting? Upcasting is the typecasting of a child object to a parent object. Upcasting can be done implicitly. Upcasting gives us the flexibility to access the parent class members but it is not possible to access all the child class members using this feature. Instead of all the members, we can access some specified members of the child class. For instance, we can access the overridden methods. Example: Let there be an animal class. There can be many different classes of animals. One such class is Fish. So, let’s assume that the fish class extends the Animal class. Therefore, the two ways of inheritance, in this case, is implemented as: Let’s understand the following code to find out the difference:
Object a Name: GoldFish Aquatic Animal Object f Name: Whale Color: Blue Aquatic Animal
An illustrative figure of the program:
- From the above example, it can be clearly understood that we can not access child class members using a parent class reference even though it is of the child type. That is:
// This statement throws // a compile-time error a.color = "Orange";
- And from the above example, we can also observe that we are able to access the parent class members and child class’s overridden methods using the same parent class reference object. That is:
// Access to child class // overridden method a.nature();
- Therefore, we can conclude that the main purpose of using these two different syntaxes is to get variation in accessing the respective members in classes.
Please Login to comment...