2018-05-01
super keyword
What is super?
this: Indicates the current object itself, or an instance of the current class, through which all methods and properties of this object can be called.
super: Represents the parent class object of the current object. ( is a reference to the parent class object in the child class object )
In the JAVA class, use super to refer to the components of the parent class, and use this to refer to the current object . If a class inherits from another class, when we new an instance object of this subclass, there will be a parent class in the subclass object. object.
How to refer to the parent class object inside it? Use super to reference, this refers to the reference of the current object, and super is the reference to the parent object in the current object.
Code example:
1 // Bird
2 class Bird
3 {
4 public void fly(){
5 System.out.println("Free fly" );
6 }
7 }
8 // Penguin
9
10 class Penguin extends Bird
11 {
12
13 public void fly(){
14 System.out.println("Angel with broken wings" );
15 }
16 public void say(){
17 System.out.println("I want to sing hahaha" );
18 super .fly(); // Use super as the reference object of the parent class object to call the fly() method in the parent class object
19 }
20 }
21
22 // Super keyword
23 class SuperDemo
24 {
25 public static void main(String[] args)
26 {
27 // Create a penguin object
28 Penguin p = new Penguin();
29 // Call the flying method
30 p.say( );
31 }
32 }
Output result:
Memory Analysis:
Reference blog: