Analysis on the Problem of subclass calling the parent class constructor in Java

Source: Internet
Author: User

In Java, the constructor of the parent class must be called during the subclass construction process because when there is an inheritance relationship, the Child class must inherit the content of the parent class, by what means?

The answer is as follows:

When you create a new subclass object, you must first create a new parent class object, which is located inside the subclass object. Therefore, the subclass object is larger than the parent class object. The subclass object contains a parent class object, which is a real situation in the memory. when the constructor is a new object, it must be called. This is a rule. To create a new parent class object, you must call its constructor. Therefore:

First rule: the constructor of its parent class must be called during subclass construction. A class. If we do not write a constructor, the compiler will add a default constructor. The so-called default constructor is a constructor without parameters, however, if you write a constructor, the compiler will not add it to you. Sometimes, when you create a new subclass object, you must call the constructor of the subclass, however, in the subclass constructor, we did not display the call base class constructor, that is, we did not write it, such as: super (); and did not write it like this, however, the constructor without parameters in the parent class will be called. If the parent class does not have any constructor, an error will occur.

The second rule: if the base class constructor is not displayed in the constructor of the subclass, the system calls the constructor of the base class without parameters by default. Note: if the base class constructor is not displayed in the constructor of the subclass, and the base class does not have a constructor without parameters by default, the compilation fails. Therefore, usually we need to display: super (Parameter List) to call the constructor with parameters in the parent class.

Copy codeThe Code is as follows: // when you do not use the default constructor of the parent class, you need to call the constructor defined by the parent class in the constructor of the subclass.
Class Animal {
Private String name;

// If you define a new Constructor
Public Animal (String name ){
This. name = name;
}
}

Public Dog extends Animal {

// You need to display the call to the constructor of the parent class, because the subclass calls the constructor of the parent class by default.
// No parameter constructor Animal ()
Public Dog (){
Super ("puppy"); // display the parameter constructor that calls the parent class

Processing the constructor of... // subclass
}
}

// Of course, if you write out the construction method without parameters in the parent class, for example:
Class Animal {
Private String name;

// Construction method without Parameters
Public Animal (){
... // Process
}

/*
If you define a new constructor, you do not need to call the constructor of the parent class in the constructor of the subclass because the subclass has a constructor without parameters,
In the constructor, subclass automatically calls the non-argument constructor defined by the parent class.
*/
Public Animal (String name ){
This. name = name;
}
}

Summary: In general, the overload of the constructor is used in the parent class, and the corresponding parent class constructor can be called in the subclass as needed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.