The Super keyword is similar to this, which is used to represent an instance of the current class, and super is used to represent the parent class.
Super can be used in subclasses, by Dot (.) To get the member variables and methods of the parent class. Super can also be used in subclass subclasses, where Java can automatically trace back to the upper class.
The behavior of the parent class is called as if the behavior is the behavior of this class, and the invocation behavior does not have to occur in the parent class, it can automatically trace back to the upper class.
Features of the Super keyword:
- Invokes a variable declared as private in the parent class.
- Take a method that has already been overridden.
- Represents the parent class constructor method as a method name.
Calling hidden variables and overridden methods
- Public class Demo{
- public static void main(String[] args) {
- dog obj = new dog();
- Obj. Move();
- }
- }
- Class Animal{
- private String desc = "Animals is Human ' s good friends";
- //must declare a getter method
- public String getdesc() { return desc; }
- public void move(){
- System. Out. println("Animals can Move");
- }
- }
- Class Dog extends Animal{
- public void move(){
- Super. Move(); //method of calling parent class
- System. Out. println("Dogs can walk and run");
- //Call the parent class to hide the variable by getter method
- System. Out. println("Please remember:" + Super. GetDesc());
- }
- }
Operation Result:
Animals can move
Dogs can walk and run
Please remember:animals is human ' s good friends
The move () method can also be defined in some ancestor classes, such as the parent class of the parent class, where Java is traceable and will always look up until the method is found.
Calling a hidden variable of the parent class through super, you must declare the Getter method in the parent class, because the data member declared as private is not visible to the child class.
Calling the parent class's constructor method
In many cases, the default constructor method is used to initialize the parent class object. Of course, you can also use super to display the constructor method that calls the parent class.
- Public class Demo{
- public static void main(String[] args) {
- dog obj = new dog("Floral", 3);
- Obj. Say();
- }
- }
- Class Animal{
- String name;
- public Animal(String name){
- this. Name = name;
- }
- }
- Class Dog extends Animal{
- int age;
- public Dog(String name, int age){
- Super(name);
- this. Age = Age;
- }
- public void say(){
- System. Out. println www.yxin7.com ("I am a cute puppy, my name is" + name + ", i" + age + "years old");
- }
- }
Operation Result:
I am a lovely puppy, my name is flower, I am 3 years old
Note: either super () or this () must be placed in the first row of the construction method.
It is worth noting that:
- Invoking another constructor method in the constructor method, the invocation action must be placed at the starting position.
- You cannot call a construction method within any method other than the constructor method.
- Only one constructor method can be called within a constructor method.
If you write a constructor method that neither calls super () nor calls this (), the compiler automatically inserts a call into the parent class constructor method without parameters.
Finally note the difference between super and this: super is not a reference to an object, and a super is not assigned to another object variable, it is just a special keyword that instructs the compiler to call the parent class method.
Four. Java inheritance and polymorphism 2. Java Super keyword