Home  >  Article  >  Java  >  Will creating a subclass object create a parent class object?

Will creating a subclass object create a parent class object?

巴扎黑
巴扎黑Original
2017-06-26 09:16:461889browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Apache Commons DigesterNext article:Apache Commons Digester