Java Object Creation of Inherited Class
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.
// A Java program to demonstrate that both super class // and subclass constructors refer to same object // super class class Fruit { public Fruit() { System.out.println("Super class constructor"); System.out.println("Super class object hashcode :" + this.hashCode()); System.out.println(this.getClass().getName()); } } // sub class class Apple extends Fruit { public Apple() { System.out.println("Subclass constructor invoked"); System.out.println("Sub class object hashcode :" + this.hashCode()); System.out.println(this.hashCode() + " " + super.hashCode()); System.out.println(this.getClass().getName() + " " + super.getClass().getName()); } } // driver class public class Test { public static void main(String[] args) { Apple myApple = new Apple(); } } |
Output:
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 contribute@geeksforgeeks.org. 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.
Recommended Posts:
- Why Constructors are not inherited in Java?
- Object class in Java
- Object level and Class level locks in Java
- Does JVM create object of Main class (the class with main())?
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Difference between Abstract Class and Concrete Class in Java
- Using predefined class name as Class or Variable name in Java



