The problem analysis of Java subclass calling Parent-Class construction method _java

Source: Internet
Author: User
Tags class definition

In Java, in the construction of subclasses, it is necessary to call the constructors of its parent class, because when there is an inheritance relationship, the subclass inherits the contents of the parent class, by what means?

The answer is as follows:

When you new a subclass object, you must first need to new a parent class of the image out, the parent object is within the subclass object, so that the subclass object is larger than the parent object, the subclass object contains a parent class object, This is the real situation in memory. The construction method is a new object, you must adjust the method, this is the rule, to the new parent object out, then definitely call its construction method, so:

First rule: During the construction of a subclass, you must call the construction method of its parent class. A class, if we don't write the construction method, then the compiler will help us add a default construction method, the so-called default construction method, is no parameters of the construction method, but if you write the construction method, then the compiler will not be added to you, so sometimes when you are new to a subclass object, Must have called the constructor method of the subclass, but in the subclass construct method We do not show the constructor method of invoking the base class, which is not written, such as: super (); This is not written, but it will call the parent class without the parameters of the constructor, if there is no constructor in the parent class without parameters will error.

The second rule: if the constructor method of the subclass does not show the calling base class constructor, then the system defaults to the constructor method of the base class without parameters Note: If the constructor method of the subclass does not show the call base class constructor, and the base class does not have a default parameterless construction method, then the compilation error occurs. Usually we need to display: Super (parameter list) to invoke the constructor with parameters of the parent class.

Copy Code code as follows:

When you do not use the default constructor method for the parent class, you need to display the constructor method that invokes the parent class definition in the constructor method of the subclass.
Class animal{
private String name;

If you define a new construction method,
Public Animal (String name) {
THIS.name = name;
}
}

Public Dog extends animal{

At this point you are going to show the constructor method of the calling parent class, because the subclass defaults to the parent class's
Non-parametric construction method animal ()
Public Dog () {
Super ("Puppy"); Shows the parameter constructor method that invokes the parent class

....//Subclass Construction Method Processing
}
}

Of course, if you write out the parameterless construction method in the parent class, such as:
Class animal{
private String name;

Non-parametric construction method
Public Animal () {
....//Handling
}

/*
If you define a new construction method, in the constructor method of the subclass, you can invoke the constructor method of the parent class without the display, because the subclass has a parameterless construction method.
Subclasses automatically invoke the parameterless construction method that the parent class has already defined in the constructor method.
*/
Public Animal (String name) {
THIS.name = name;
}
}

Summary: However, in general, the overload of the constructor method is used in the parent class, and the corresponding parent class construction method can be invoked as needed in the subclass.

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.