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.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Upcasting Vs Downcasting in Java
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.util.BitSet class in Java with Examples | Set 1
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.frequency() in Java with Examples
- Java.util.Arrays.equals() in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java 8 | Consumer Interface in Java with Examples
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java 8 | ArrayDeque removeIf() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java Clock tickMinutes() method in Java with Examples
- Java Clock withZone() method in Java with Examples
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java 8 | BiConsumer Interface in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.