Super has the following two major functions:
1. Qualifying : force access to members of the parent class (Field, method)
2. Call function : Specify/Display a constructor that invokes the parent class
Super Call rule:
1, the subclass constructor will always call the parent class constructor once , by default, the class constructor automatically calls the parent class without parameters of the constructor ;
2, if you want to explicitly specify which constructor the subclass constructor calls the parent class , you can use super (parameter, parameter,....), which parent constructor is called depending on the list of parameters passed in after Super ;
3, this () Call and super () call must appear in the first row of the constructor, so they cannot both be present at the time of the call.
Conventional:
1, if there is no super () call, nor the This () call, the default subclass constructor will automatically call the parent class parameterless constructor ;
2, if there is a super () call, according to Super () call the incoming parameter list to decide which parent class constructor to call ;
3. If there is this () call, the subclass constructor finds the constructor that is overloaded in the corresponding subclass of the This () call.
Example 1: Using Super in a method. Limiting function
1 classfruit{2 PrivateString color;3 Private Doubleweight;4 5 PublicFruit () {}6 PublicFruit (String color,Doubleweight) {7 This. color =color;8 This. Weight =weight;9 }Ten One Public voidinfo () { ASystem.out.println ("This is a fruit Oh!")); -SYSTEM.OUT.PRINTLN ("Fruit color is:" + color + ", Weight:" +weight); - } the } - - - Public classAppleextendsfruit{ + Public voidinfo () { -System.out.println ("This is an apple!")); + } A at Public voidTest () { -System.out.println ("~~apple test Method"); - //This.info ();-through this. Call info by default always calls the info () method defined in this class - Super. info ();//-Enforce calls to the info () method in the parent class by Super. - } - in Public Static voidMain (string[] args) { -Apple A =NewApple (); to a.test (); + } -}
Example 2: Use Super in field. As well as a limiting function
1 classdemo{2 intCount = 2;3 }4 5 Public classTestdemoextendsdemo{6 intCount = 10;7 Public voidTest () {8 //The default count (that is, count does not add this./super.) , the This is omitted by default in front of the9 //If you must use the Count field of the parent class, precede it with super.TenSYSTEM.OUT.PRINTLN ("The test method accesses the count of the current class:" + This. Count); OneSYSTEM.OUT.PRINTLN ("The test method accesses the count of the parent class:" +Super. Count); A } - - Public Static voidMain (string[] args) { theTestdemo T =NewTestdemo (); - t.test (); - } -}
Example 3: Calling the parent class constructor through super
1 classperson{2 protectedString name;3 protected intAge ;4 protectedString sex;5 6 //The programmer defines a parametric constructor, so the system no longer generates a parameterless constructor for that class7 PublicPerson (String name,intAge , String Sex) {8 This. Name =name;9 This. Age =Age ;Ten This. Sex =sex; One } A - //If you do not define a method, a call to the Name/age/sex parameter in the main method compiles with an error "the Staic method cannot reference a non-static variable" - Public voidinfo () { theSystem.out.println ("The teacher's name is called:" + name + ", Age:" + ages + ", Gender:" +sex); - } - } - + Public classTeacherextendsperson{ - PrivateString sex; + A //If you do not specify a constructor, the default constructor for the parent class is called, but the parent class does not have a parameterless constructor to make an error . at PublicTeacher (String name,intAge , String Sex) { - Super(name,age,sex); - } - - - Public Static voidMain (string[] args) { inTeacher TT =NewTeacher ("haha", "male"); - tt.info (); to } +}
Crazy Java Learning Notes Object-oriented (vii)-SUPER keyword