super is the keyword used to access super class members in Java. The specific usage is: calling the method in the super class: super.method_name() accessing the variable in the super class: super.variable_name calling the super class Constructor: super(arguments)
Usage of super in Java
What is super?
super is a keyword in Java used to access members of a super class. It refers to the immediate superclass of the class that calls it.
Usage of super
There are three main uses of super:
When to use super
Common situations where super is used include:
Example
<code class="java">class SuperClass { int value = 10; void printValue() { System.out.println("SuperClass value: " + value); } } class SubClass extends SuperClass { int value = 20; @Override void printValue() { super.printValue(); // 调用超类中的 printValue 方法 System.out.println("SubClass value: " + value); } } public class Main { public static void main(String[] args) { SubClass obj = new SubClass(); obj.printValue(); // 输出: // SuperClass value: 10 // SubClass value: 20 } }</code>
Note:
The above is the detailed content of How to use super in java. For more information, please follow other related articles on the PHP Chinese website!