In inheritance, subclass acquires super class properties. An important point to note is, when subclass object is created, a separate object of super class object will not be created. Only a subclass object object is created that has super class variables.
This situation is different from a normal assumption that a constructor call means an object of the class is created, so we can’t blindly say that whenever a class constructor is executed, object of that class is created or not.
super class constructor super class object hashcode :366712642 Apple sub class constructor sub class object hashcode :366712642 366712642 366712642 Apple Apple
As we can see that both super class(Fruit) object hashcode and subclass(Apple) object hashcode are same, so only one object is created. This object is of class Apple(subclass) as when we try to print name of class which object is created, it is printing Apple which is subclass.
This article is contributed by Gaurav Miglani. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Why Constructors are not inherited in Java?
- Object Serialization with Inheritance in Java
- More restrictive access to a derived class method in Java
- Using the super Keyword to Call a Base Class Constructor in Java
- Inheritance and constructors in Java
- Comparison of Inheritance in C++ and Java
- Accessing Grandparent’s member in Java using super
- Java and Multiple Inheritance
- Overriding in Java
- Covariant return types in Java
- Inheritance in Java
- Dynamic Method Dispatch or Runtime Polymorphism in Java
- Association, Composition and Aggregation in Java
- Using final with Inheritance in Java
- Interfaces and Inheritance in Java
- Java | Inheritance | Question 1
- Java | Inheritance | Question 2
- Java | Inheritance | Question 3
- Java | Inheritance | Question 4
- Java | Inheritance | Question 5