java keyword (vi)--super

Source: Internet
Author: User

In the Java keyword (v)--this We say that the This keyword is a reference to the current object. The Super keyword in Java is a reference to an object that represents the parent class.

  We analyze the phrase "reference to the parent class object", which means that we use it only in subclasses, and since it is a reference to an object, we can also invoke member properties and member methods, and of course, the Super keyword here can also invoke the constructor of the parent class. There are several uses:

1. Calling the parent class's construction method

Inheritance in Java Everyone should understand that subclasses inherit the parent class, we are able to invoke the parent class's properties and methods with the subclass object, we know that the properties and methods can only be called through the object, then we can boldly assume that:

When you create a subclass object and you create an object of the parent class, and the object is created by calling the constructor, we should call the constructor of the parent class when we create the child class object.

Let's look at this code:

1  Public classParent {2 3      PublicParent () {4System.out.println ("Parent class default No parameter construction method");5     }6 }7 8 9  Public classSonextendsParent {Ten  One      PublicSon () { ASystem.out.println ("Subclass default non-parametric constructor method"); -     } -}

Here we create the object of the subclass:

1      Public Static void Main (string[] args) {2         New Son (); 3     }

Printing results:

  

When we create the subclass object by printing the result, we call the construction method of the parent class first, and then call the constructor of the subclass, that is, when we create the subclass object, we first create the parent class object, which is the same as we guessed before.

So the question comes again: When is the parent constructor method called?

Refer to the official Java documentation: https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#d5e14278

  

  

The English translation in the Red box is: If the declared class is the original class Object , then the default constructor has an empty body. Otherwise, the default constructor simply calls the superclass constructor with no parameters.

That is, except that the top-level class Object.class constructor does not call the constructor of the parent class, all the rest of the classes call the constructor of the parent class by default in the constructor (no subclass of the parent class is explicitly declared as Object).

So what is it called by? Let's continue with the official documentation:

The above means that the superclass constructor is called by the Super keyword and begins with the Super keyword.

So the method of constructing the Son class above should actually be this:

  

①, subclass by default, is called by Super () to call the parent class of the parameterless construction method, if the parent class display declares an argument constructed method, but does not declare the parameterless constructor method, the instantiation subclass will be an error.

1  Public classParent {2 3      PublicParent (String name) {4System.out.println ("Parent class has a constructor method");5     }6 }7 8  Public classSonextendsParent {9 Ten      PublicSon () { OneSystem.out.println ("Subclass default non-parametric constructor method"); A     } -  -      Public Static voidMain (string[] args) { theSon son =NewSon (); -     } -  -}

The above code will be an error:

  

The workaround is to invoke the parent class's argument-building method through the Super keyword:

1  Public classSonextendsParent {2 3      PublicSon () {4         Super("Tom");5System.out.println ("Subclass default non-parametric constructor method");6     }7 8      Public Static voidMain (string[] args) {9Son son =NewSon ();Ten     } One  A}

  Note that the 4th line of code, in the same way, multiple parameters are also the same.

2. Calling the parent class's member property
1  Public classParent {2      PublicString name;3 4      PublicParent () {5System.out.println ("Parent class default No parameter construction method");6     }7 }8 9  Public classSonextendsParent {Ten  One      PublicSon () { ASystem.out.println ("Subclass default non-parametric constructor method"); -     } -  the      Public voidPrintname () { -System.out.println (Super. Name); -     } -  +}

The 16th line of code Super. The parent class property invokes the property of the parent class in this form.

3. Calling the parent class method
1  Public classParent {2      PublicString name;3 4      PublicParent () {5System.out.println ("Parent class default No parameter construction method");6     }7 8      Public voidsetName (String name) {9          This. Name =name;Ten     } One } A  -  Public classSonextendsParent { -  the      PublicSon () { -         Super();//1. Calling the parent class constructor -System.out.println ("Subclass default non-parametric constructor method"); -     } +  -      Public voidPrintname () { +         Super. SetName ("Tom");//2. Calling the parent class method ASystem.out.println (Super. name);//3. Calling the parent class Property at     } -  -      Public Static voidMain (string[] args) { -Son son =NewSon (); -Son.printname ();//Tom -     } in  -}

In this example we call the constructor, normal method, and member properties of the parent class, respectively, in the subclass.

4, this and super appear in the same constructor method?

No!!!

In the previous blog's introduction to the This keyword, we know that you can call your own construction method through the This keyword. And this blog introduces the Super keyword, we know the ability to call the parent class through Super constructor, then these two keywords can appear in the subclass of the construction method?

  ①, assuming super () in front of the This () keyword

The parent class is first instantiated with a super () call to construct the method. The This () method is then called, which invokes the constructor of the subclass, and the parent class is instantiated once in the constructor method of the subclass. That is to say, we instantiate a subclass once, which causes the parent class to be instantiated two times, so the compiler is obviously not allowed.

1  Public classParent {2      PublicString name;3 4      PublicParent () {5System.out.println ("Parent class default No parameter construction method");6     }7 8      PublicParent (String name) {9System.out.println ("Parent class has a constructor method");Ten     } One  A } -  -  Public classSonextendsParent { the  -      PublicSon () { -         Super();//1. Calling the parent class constructor -          This("Tom");//2. Call Subclass Construction Method +System.out.println ("Subclass default non-parametric constructor method"); -     } +  A      PublicSon (String name) { atSystem.out.println ("Sub-class has a method of constructing a parameter"); -     } -  -}

This, in turn, is the same as before Super ().

And the compiler has qualified this () and super () These two keywords can only appear in the first line of construction method, put these two keywords together, there is always a keyword in the second line, compilation is not passed.

java keyword (vi)--super

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.