From simple things, I will output java steadily. I am studying with the goal of passing java silver.
Used when referencing superclass instances and variables.
super. method name
Main.java
//Super class
class hoge {
public void print() {
System.out.println("hello from superclass");
}
}
//Child class
class huga extends hoge {
public void print() {
System.out.println("hello from child class");
}
public void executePrint() {
//Child class print
print();
//Superclass print
super.print();
}
}
public class Main {
public static void main(String[] args) {
huga obj = new huga();
obj.executePrint();
}
}
Execution result
hello from child class
hello from superclass
You can see that the method of the parent class can be explicitly called from the child class.
In the case of a constructor, if you instantiate a child class
Superclass constructor
→ Child class constructor
is executed.
Details will be written in another article.
This article was written with reference to the following information.
-Oracle Certification Textbook Java Programmer Silver SE11
Recommended Posts