"Simple Java" about the construction methods of the Java subclass and the parent class

Source: Internet
Author: User

This article summarizes some common questions about Java construction methods.

Why a subclass object is created calls its parent class's constructor method

The following code example:

 PackageSimplejava;classSuper {String s;  PublicSuper () {System.out.println ("Super"); }}classSubextendsSuper { PublicSub () {System.out.println ("Sub"); }} Public classQ4 { Public Static voidMain (string[] args) {Sub s=NewSub (); }}

Results Print:

Super
Sub

When a class inherits a class, in the constructor of the subclass, super () must be called first, and if you do not write, the compiler automatically calls the super () method, which calls the constructor of the parent class;

This is not the creation of two objects, there is only one subclass sub object, and the reason to call the parent class is because in the parent class, there may be a private property that needs to be initialized within its constructor method;

When the compiler automatically inserts a call to the parent class construction method, the child class is constructed like this code:

     Public Sub () {        Super();        System.out.println ("Sub");    }
A common error message: Implicit super constructor is undefined for default constructor

For Java developers, this is a very common compilation error:

"Implicit super constructor is undefined for default constructor. Must define an explicit constructor "

This compilation error occurs because the default constructor method of the parent class is undefined. In Java, if a class does not have a constructor defined, the compiler inserts a parameterless constructor by default, but if a constructor is defined in the parent class, in which case the compiler does not automatically insert a default parameterless construction method, which is exactly the case of the above demo;

For subclasses, the non-parametric construction method of the parent class is called by default, regardless of whether it is a parameterless constructor or a constructor method, and when the compiler attempts to insert the super () method into both constructors in a subclass, the compiler will give an error because the parent class does not have a default parameterless constructor method.

To fix this error, it's simple:

1. Manually define a non-parametric construction method in the parent class:

 Public super () {    System.out.println ("Super");}

2. Remove the custom construction method from the parent class

3, in the subclass of their own write on the parent class construction method call, such as super (value);

Explicitly calling the parent class construction method in a subclass

The following code works correctly:

The parent class defines a method for constructing a parameter, and then the subclass construction method explicitly calls the parent class to construct a method;

Rule Summary

To put it simply, the rule is this:

In the subclass construction method, either the programmer manually calls the parent class constructor method or the compiler calls it automatically.

The subclass constructor method must call a constructor of the parent class;

The parent class constructor method called by the quilt must exist in the parent class;

An interesting question.

If a class defines a constructor that has parameters, why does Java not provide a default parameterless construction method?

Interested can look at the answer in the StackOverflow:

http://stackoverflow.com/q/16046200/127859

Link: http://www.programcreek.com/2013/04/what-are-the-frequently-asked-questions-about-constructors-in-java/

Simple Java constructs methods for subclasses and parents of Java

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.