How to construct Java in inheritance

Source: Internet
Author: User

How to construct in inheritance

1. The constructor of the subclass must be called during the construction of its base class.

2. Subclasses can use Super (argument_list) to call the constructor of the base class in their own construction method.

2.1. Use this (argument_list) to call the other construction methods of this class.

2.2. If you call super, you must write the first line of the subclass construction method.

3, if the constructor of the subclass is not shown in the constructor method of the call base class, the system defaults to calling the base class's parameterless construction method.

4, if the subclass constructor method does not display the call base class construction method, and the base class does not have parameterless construction method, then the compilation error.

classsuperclass{Private intN; //Superclass () {//System.out.println ("superclass ()"); //}Superclass (intN) {System.out.println ("Superclass (int n)");  This. N =N; }}classSubclassextendssuperclass{Private intN; Subclass () {Super(300); System.out.println ("Superclass"); } Subclass (intN) {System.out.println ("Subclass (int N):" +N);  This. N =N; }} Public classtestsupersub{ Public Static voidMain (String args[]) {//Subclass SC = new subclass ();Subclass SC2 =NewSubclass (200); }}

Verify the above syntax in turn.

How to construct Java in inheritance

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.