The super keyword in Java is used to access the constructor, methods and fields of the parent class: Member access: super() calls the parent class constructor. Method access: super.method() calls the parent class method. Field access: super.field accesses parent class fields.
super in Java
super is a keyword in Java that is used to access members of the parent class . In a subclass, use the super keyword to access the constructor, methods, and fields of the parent class.
Member access
Usage
The super keyword is usually used in the following situations:
Example
<code class="java">class Parent { private int age; public Parent(int age) { this.age = age; } public int getAge() { return age; } } class Child extends Parent { public Child(int age) { super(age); // 调用父类构造函数 } @Override public int getAge() { return super.getAge() + 1; // 覆盖父类方法并调用父类实现 } }</code>
In this example, the subclass Child initializes the age field of the parent class by calling the constructor of the parent class Parent through super(age) . It also achieves polymorphism by calling the getAge() method of the parent class through super.getAge().
The above is the detailed content of What does super mean in java. For more information, please follow other related articles on the PHP Chinese website!