Will creating a subclass object create a parent class object?
No, only a subclass object is created, but the subclass object is passed to the constructor of the parent class object. The address; when initializing the subclass object, the constructor of the parent class is called.
Proof:
class A{ public A(){ System.out.println("A=="+this.hashCode()); } } class B extends A{ public B(){ System.out.println("B=="+this.hashCode()); } } public class test{ public static void main(String[] args){ A test=new B(); } }
Result:
A==366712642
B==366712642
If the subclass object is created at the same time A parent class object is also created, so the hashcode (memory address) of this in the parent class and child class construction methods will be different, but the result will be the same.
The above is the detailed content of Will creating a subclass object create a parent class object?. For more information, please follow other related articles on the PHP Chinese website!