Hero.java
public class Hero {
String name = "Brave";
int hp = 100;
//escape
public void run() {
System.out.println(this.name + "Escaped!");
}
}
SuperHero.java
public class SuperHero extends Hero{
boolean flying;
//Withdraw
public void run() { //It is also defined in the Hero class, but it can be redefined (overwritten)
System.out.println(this.name + "Withdrew!");
}
}
Main.java
public class Main {
public static void main(String[] args) {
Hero h = new Hero();
h.run();
SuperHero sh = new SuperHero();
sh.run();
}
}
Execution result The brave escaped! The brave has withdrawn!
Main.java
public class Main extends String{
String class prohibition can also be confirmed in java API reference
Main.java
public final class Main {
Hero.java
public class Hero {
String name = "Brave";
int hp = 100;
//escape
public final void run() { //run()Cannot be overridden
System.out.println(this.name + "Escaped!");
}
}
public final void run ()
The run () method of the SuperHero class mentioned earlier becomes invalid.
Recommended Posts