How to construct in Java inheritance

Source: Internet
Author: User

how to construct in inheritance://A inherits from B//1. Before new A A, you need to first construct a B//2. The "super ()" keyword is used to construct B in the construction method of a (the Super keyword needs to be placed in the first row of the construction method, since B is constructed before the next one is constructed)//3. When the Super keyword is not written in the A constructor method, the compiler automatically calls the parent class without the parameter constructor (regardless of whether the parent class has no Override)//4.super () different construction methods for parent class according to incoming parameters//5.super () in a multi-layer subclass is just a construction method in a layer-up class. For example: Public classclas1{ Public Static voidMain (string[] args) {C dx=NewC ();//Call Class C    }}classA//Parent Class{        intA;  PublicA ()//non-parametric construction{System.out.println ("This is the construction method of a 1"); }                 PublicAintSZ)//Pass-Parameter construction{System.out.println ("This is the construction method of a 2"); }    }classBextendsA//b inherits from A, is the subclass of a{        intb;  PublicB ()//non-parametric construction            {            Super();//running a class-a non-parametric construction method            }             PublicBintSZ2)//Pass-Parameter construction            {            Super(22);//To run a Class A-pass parameter construction method            }        }classCextendsB//C inherits from B and is a subclass of B{    intC;  PublicC ()//C-Class construction method            {            Super(3);//Run Class B construction method, choose different construction methods depending on the parameters passed in            }}

How to construct in Java 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.