Home > Java > javaTutorial > body text

Will creating a subclass object create a parent class object?

巴扎黑
Release: 2017-06-26 09:16:46
Original
1889 people have browsed it

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();

    }

}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!