Java Super keyword

Source: Internet
Author: User
Tags instance method

We already know that if a member variable defined in a subclass has the same name as a member variable in the parent class, the member variable in the parent class cannot be inherited, and the member variable of the child class is called the member variable of the parent class is hidden.

When a method is defined in a subclass, and the method's name, return type, number of arguments, and a method of the parent class are exactly the same, this method of the parent class is hidden (rewritten), neither quilt

class inherits. You can use the keyword super if we want to use member variables or methods of the parent class hidden by the quilt class in the subclass. Therefore, super is used in subclasses to access the direct parent class that is masked

, note that it is the immediate parent class (the closest superclass above the class).

1 Use Super calling the parent class's constructor method

Subclasses do not inherit the construction method of the parent class. Therefore, subclasses that want to use the constructor of the parent class must be used in the constructor of the subclass and must be represented using the keyword super. and super must be

The subclass constructs the first statement in the method (that is, the parent class must be responsible for initializing her own state instead of having the subclass do it).

It is important to note that if a constructor method of the parent class is not displayed using the Super keyword in the constructor method of the subclass, then there is a super (), and the statement, which calls the parent class without parameters, constructs

Manufacturing methods. If the parent class does not provide a constructor with no arguments, an error occurs. Special Note: When there is a parameter constructor, the parameterless constructor does not exist, and the declaration must be displayed as an empty constructor.

2 Use Super manipulating member variables and methods that are hidden

The keyword super can be used if we want to use member variables or methods of the parent class hidden by the quilt class in the subclass. such as Super.x,super.play (), is the quilt class hidden parent class member variable x and method play ().

The keyword super and inheritance together establish a close relationship between the class and its parent class. For example, when we are invoking an instance method, if the instance itself does not define the method, then naturally we will get the method with the same name defined in its parent class.

This access is lost even though it is overridden by methods or uses an instance or class variable (called "hidden") that defines the same as the parent class. This is why you use the Super keyword, which explicitly states that subclasses can access certain parts of the parent class directly.

Although sometimes this access is masked for various reasons, the original code in its parent class.

The keyword super is very important in the use of constructors, and unlike methods, constructors are not inherited; So super is the only way to access constructors in the parent class. In the constructor of the child class,

Using super () and the appropriate parameter table can trigger a call to the parent class constructor, and if the parent does not have a corresponding constructor, the compiler will make an error, which is the process chain of the initialization of each instance implementation.

The instance initializes itself as an Object instance and then calls the constructor from its immediate subclass to the inheritance chain until it finally initializes the content directly related to the current class.

Subclass constructors If you want to refer to super, you must put super in the first place of the function, or you will get an error like this:
Checket.java:10:call to Super must is first statement inconstructor

So when calling the parent class constructor with super in the class, why does the calling statement have to be the first statement of the subclass?

Answer: If you want to use super to inherit the method constructed by the parent class, but not on the first line, then the statement before super is definitely to satisfy the statement that you want to complete some behavior, but also uses super to inherit the constructor of the parent class. So the changes we've made before have gone back to the way they were constructed.

Here's a summary of the use of super:
First, in the subclass construction method to invoke the parent class's constructor method, called with "super (parameter list)", the parameter is not necessary.

It is also important to note that the "super (parameter list)" statement can only be used in the first row of the subclass in which the method body is constructed.
Second, when a local variable in a subclass method or a member variable of a subclass has the same name as a parent class member variable, that is, when the child class local variable overrides the parent class member variable,

Use "super. Member variable name" to refer to the parent class member variable. Of course, if the member variables of the parent class are not overwritten, you can also refer to the parent class member variable with "super. Member variable name", but this is unnecessary.
Thirdly, when the member method of a subclass overrides the member method of the parent class, that is, the subclass and parent class have exactly the same method definition (but the method body can be different), at this point, the method of the parent class is accessed with "super. Method name (parameter list)".
The Super keyword can only be used in non-static parts of the class body, such as constructors and member methods, if a call in the main function or a static method in the compilation will be wrong, reported cannot use super in a static context error!

Note: The properties and methods of the parent class must be which protected or public, such as properties or methods that can be accessed by subclasses.

Java Super keyword

Related Article

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.