Explanation of the super call in the subclass constructor of the parent class private property

Source: Internet
Author: User

 Packagetest; Public classCar {Private intCarmoney;//Car Rental        PrivateString Carname;//Car name        Private intCarnumb;//Car Serial number        Private intCarrynum;//Cargo Capacity        Private intPassengernum;//number of passengers                  PublicCar (intCarnumb,string Carname,intCarmoney,intPassengernum,intcarrynum) {             This. Carmoney =Carmoney;  This. Carname =Carname;  This. Carnumb =Carnumb;  This. Carrynum =Carrynum;  This. Passengernum =Passengernum; }         Public intGetcarmoney () {returnCarmoney; }         Public voidSetcarmoney (intCarmoney) {             This. Carmoney =Carmoney; }         PublicString Getcarname () {returnCarname; }         Public voidsetcarname (String carname) { This. Carname =Carname; }         Public intGetcarnumb () {returnCarnumb; }         Public voidSetcarnumb (intcarnumb) {             This. Carnumb =Carnumb; }         Public intGetcarrynum () {returnCarrynum; }         Public voidSetcarrynum (intcarrynum) {             This. Carrynum =Carrynum; }         Public intGetpassengernum () {returnPassengernum; }         Public voidSetpassengernum (intpassengernum) {             This. Passengernum =Passengernum; }                     Public voidShowinfo () {//TODO auto-generated Method Stub        }               Public Static voidMain (string[] args) {Car as=NewCar (0, "Sende", 200000,23,32);            System.out.println (As.carmoney); /*** * Subclasses can inherit the variables of the parent class when the child class and the parent class are in the same package, the subclass can inherit all the methods and member variables of the parent class except the private property, and when the subclass is not in the same package as the parent class, the subclass only *①             member variables and methods that can inherit the protected and public properties of the parent class.             When a member variable defined in the *② subclass has the same name as a member variable in the parent class, the subclass hides the inherited member variable, a method is defined in the subclass, and the method's name, return type, number of arguments, and type are exactly the same as the method inherited from the parent class, which hides the methods inherited from the parent class (override of the method             *③ allows a method in a subclass to have the same name as the parent class and different types. * * In this test class you can see that you can use the class object directly to get the property in the parent class, even if the property is a private property, and the object using its child class gets the corresponding property, but the object name cannot be used. Properties, * can only be obtained by the public Get method, you see In the constructor of a subclass, the variable is not a subclass, but a parent class, and the Super keyword is called by the constructor of the parent class, and must be placed in the first row of the constructor * This and super are divided into invocations and references. Only the parent class constructor can be referenced and cannot be called.             A reference can only appear in the first row of the constructor. * Super refers to the parent class's constructor instead of the parent class's property, and if you make the parent class's construction method private you can see that the subclass's construct with super is wrong. Because super calls the constructor of the parent class *, so in the parent class constructor The this represents the parent class, the assigned attribute is also the parent class, where the sense of inheritance in the subclass is a mapping of the parent class property, because it is affected by the modifier * words in the parent class. All belong to sub-class * Do not know correct incorrect, my view, welcome to point out the problem, thank you*/Sedan SD=NewSedan (0, "Sende", 200000,23,32);                    System.out.println (Sd.getcarnumb ()); }        }          classSedanextendscar{ PublicSedan (intCarnumb,string Carname,intCarmoney,intPassengernum,intcarrynum) {            Super(Carnumb,carname,carmoney,passengernum,carrynum); }    }         classPickupextendscar{ PublicPickup (intCarnumb,string Carname,intCarmoney,intPassengernum,intcarrynum) {            Super(Carnumb,carname,carmoney,passengernum,carrynum); }    }

Explanation of the super call in the subclass constructor of the parent class private property

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.