The construction method of the topic summarization of Java Technology

Source: Internet
Author: User

The inheritance mechanism of a class enables subclasses to use the functionality (that is, code) of the parent class, and the subclass also has the type of the parent class. The following describes the sequence of initialization of a class on an inheritance relationship.

Example 1:

class SuperClass
{
SuperClass()
{
System.out.println("SuperClass constructor");
}
}
public class SubClass extends SuperClass
{
SubClass()
{
System.out.println("SubClass constructor");
}
public static void main(String[] args)
{
SubClass sub = new SubClass();
}
}
输出结果: SuperClass
constructor
SubClass constructor

Only one subclass object is instantiated in a subclass. From the output, the program does not run its own method of construction at the outset, but rather runs the default constructor of its parent class first. Note: The program automatically invokes the default constructor for its parent class.

Example 2:

class SuperClass
{
SuperClass(String str)
{
System.out.println("Super with a string.");
}
}
public class SubClass extends SuperClass
{
SubClass(String str)
{
System.out.println("Sub with a string.");
}
public static void main(String[] args)
{
SubClass sub = new SubClass("sub");
}
}

Compiling this program under JDK is not successful. As the previous example said: The program to initialize the subclass of the first to find the default construction method of its parent class, the result is not found, then the compilation naturally cannot pass.

There are two ways to solve this problem:

1. Adds a default construction method to the parent class.

2. Add a statement to the constructor method of the subclass: Super (STR); and must be in the first sentence.

Both of these methods enable this program to be compiled, but the results are not the same for this program.

The 1th method runs the following results:

Sub with a string.

The 2nd method runs the following results:

Super with a string.
Sub with a string.

The 2nd solution is actually to specify that the compiler should not look for the default constructor for the parent class, but instead look for a constructor with a string as a parameter.

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.