Android (Java) Learning Note 119: The parent class in inheritance has no parameterless constructs

Source: Internet
Author: User

1 /*2 if the parent class does not have a parameterless constructor, what happens to the subclass's constructor?3 error. 4 How to solve it? 5 A: add a non-parametric construction method to the parent class 6 B: Call the parent class by using the Super keyword to display the parameter construction method 7 C: subclasses use this to invoke the other constructor methods of this class 8 The subclass must have a constructor that accesses the parent class, otherwise the parent data is not initialized. 9             Ten Precautions : One This (...) or Super (...) Must appear on the first statement.  A If it is not placed on the first statement, it is possible to initialize the data of the parent class more than once, so it must be placed on the first statement.  - */ - classFather { the     /* - Public Father () { - System.out.println ("Father-free method of construction"); -     } +     */ -     //If there is no write construction method, there is no parameter structure by default, the structure of the parameter is written here, and the non-parametric construction becomes the parameter structure. +      PublicFather (String name) { ASystem.out.println ("Father method for the construction of parameters"); at     } - } -  - classSonextendsFather { -      PublicSon () { -         Super("Give Whatever"); inSystem.out.println ("Son's method of non-argument construction"); -         //super ("Give casually"); to     } +      -      PublicSon (String name) { the         //super ("Give casually"); *          This(); $System.out.println ("The method of the construction of the son with reference");Panax Notoginseng     } - } the  + classExtendsDemo7 { A      Public Static voidMain (string[] args) { theSon s =NewSon (); +System.out.println ("----------------"); -Son SS =NewSon ("Brigitte"); $     } $}

When the subclass son creates an object, whether it uses the parameterless construct or the parameter construct, that is, whether to use new Son (), new son ("Brigitte"), here the default first statement in these constructor methods must implicitly default to Super (), which is the non-parametric construction method of the parent class. Use the parameterless constructor of the parent class to initialize the data carried by the parent class and provide it to the subclass son. Usually, if the parent class does not write constructs, the system default parent class is a parameterless construct, but if the parent class writes a parameter construct, then the parameterless construct will be overwritten so that the parent class father () will have no parameterless constructs, so that the previous son () creates an object with an error. However, you can also explicitly use super ("name") to call the parent class with the parameter construct class.

Android (Java) Learning Note 119: The parent class in inheritance has no parameterless constructs

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.