Will the Java subclass inherit the constructor of the parent class?

Source: Internet
Author: User

Statement: Just touch Java soon, if you understand there are errors or deviations look at the strong criticism of the big guy

Can the Java subclass inherit the constructor of the parent class?

Parent Class Code:

1 classFather {2String name;//it's not set/get.3     //Non-parametric construction method of Father class4      PublicFather () {5System.out.println ("This is a non-parametric construction method of the Father class");6     }7     //The method of father class with parameter construction8      PublicFather (String name) {9          This. Name =name;TenSystem.out.println ("This is a method of the Father class:") This. Name); One     } A      -}

Sub-class Code:

1 class extends Father {2// Here Java will provide you with a default construction method, but the code is not displayed 3// public Sun () {}4 }

So run the test class, create the parent class object, and call the no-argument constructor method class.

Results:

1  Public class FatherTest01 {2      Public Static void Main (string[] args) {3         New Father (); 4         5         // Sun SS = new Sun (); 6     }7 }

The results are as follows: "The output is shown in red."

This is a non-parametric construction method for the Father class

So run the test class, create the parent class object, call the argument constructor method class.

Father ff = new Father ("Xiaoming");

  

The results are as follows: "The output is shown in red."

This is the father method of the class: Xiao Ming

We then create the subclass: The Sun Class object, which calls the parameterless constructor method class.

Sun SS = new Sun ();

The results are as follows: "The output is shown in red."

This is a non-parametric construction method for the Father class

It can be seen that the subclass object calls the parent class's parameterless constructor by default, because there is no constructor in the Sun class, so Java defaults to the parameterless constructor method.

Public Sun () {

This constructor also defaults to calling the parent class's parameterless construction method, which is public Father () {

System.out.println ("This is a non-parametric construction method of the Father Class");

}

So the result is the same as the result of creating a sun class parameterless construction method:

Public Sun () {

}

Verify again:

The non-parametric construction method in the Sun class is written like this:

Class Sun extends Father {//string name = "Sun";p ublic Sun () {System.out.println ("This is the non-parametric construction method of the Sun class");}}

Result: "This will call the constructor of the parent class first, so the result will first appear in the parent class without a parameter in the constructor method of the statement, that is, the implicit statement: Super ();"

This is a non-parametric construction method for the Father class
This is the non-parametric construction method of the Sun class

Therefore, the constructor method is a class-specific method that is not inherited by another class, but when the subclass instantiates the object, if the constructor of the child class does not explicitly call the parent class constructor, the default parameterless constructor for the parent class is automatically called, which is equivalent to omitting super () by default;

Suppose the parent class has only a constructor method, what happens if there is no parameterless construction method?

Class Father {String name;//does not set/get the//father class parameterless construction method/*public Father () {System.out.println ("This is the non-parametric construction method of the Father class");} *///father class has a constructor method public Father (String name) {this.name = name; System.out.println ("This is the father method of the class:" +this.name);}}

When you run the idiom, the compiler will error: the constructor Father () is undefined

Summarize:

 When a subclass instantiates an object, the constructor of the parent class is called first, and if the subclass's constructor does not explicitly call the constructor of the parent class, it calls Super () by default;
Subclasses want to use the constructor method of the parent class with arguments, use super (parameter) Form, and super () must be the first line of the child class construction method.
If the parent class does not have a constructor with no arguments, and there are no explicit calls to the parent class in the constructor method of the child class, the Java compiler will error

In addition: methods are not inherited one said, only overrides or overloads, between classes and classes can be called inheritance.


Remark: Constructor = = Constructor method

Will the Java subclass inherit the constructor of the parent class?

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.