Relationships between constructors during Java inheritance

Source: Internet
Author: User

 

The relationship between constructors during Java inheritance. Let's take a test to show you:

 

In inheritance, which of the following statements about constructor is false? () A. Subclass unconditionally inherits the non-argument constructor of the parent class, b. Subclass can reference the constructor with parameters in the parent class and use the super keyword. C. If the constructor does not have a constructor, the parent class has no constructor as its own constructor,

D. If the child class has no constructor, and the parent class has no constructor, the constructor will be overwritten.

Let's take a look at two examples:

1 public class parent {
2 public static void main (string ARGs []) {
3
4}
5 Public parent (){
6 system. Out. println ("parent class ");
7}

8}

 

1 public class testextends extends parent {
2 public static void main (string ARGs []) {
3 testextends tes = new testextends ();
4 tes. testmode ();
5}
6 Public testextends (){
7 system. Out. println ("testextends class ");
8}
9
10 public void testmode (){
11 system. Out. Print (100% 3 );
12 system. Out. Print (",");
13 system. Out. println (100% 3.5 );
14}

15}

 

Output result:

Parent class

Testextends class1. 2.0

Conclusion It is obvious that the non-parameter constructor of the subclass will not overwrite the parent class, And will first call the constructor of the parent class before calling its own.

 

 

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.