** Abstract class master ball (top) ** ① All methods are abstract methods ② Basically it does not have a field
Human.java
public abstract class Human { //Meet ①② above
public abstract void run();
}
Human.java
public interface Human {
void run(); //public abstract can be omitted
}
Hero.java
public class Hero implements Human {
}
Hero.java
public class PrincessHero
implements Hero, Princess { //Can inherit multiple parent interfaces
}
Multiple inheritance is not possible in classes, but it is possible in interfaces
Hero.java
public interface Human extend Creature {
void talk();
void watch();
void hear();
//In addition, run from the parent interface()Inheriting
}
----- ʻextend (inheritance) between classes and interfaces --If you use an interface in a class (heterogeneous), ʻimplements
Hero.java
public class Hero extends Charaater implements Human{
//Inherit fields such as hp and name from Charaater
//Abstract method attack inherited from Charaater()Implementation
public void attack(Goblin g) {
System.out.println(this.name + "Inflicted 5 damage");
}
//In addition, implement four abstract methods inherited from Human
public void talk(){
System.out.println("・ ・ ・");
}
public void watch(){
System.out.println("・ ・ ・");
}
public void hear(){
System.out.println("・ ・ ・");
}
public void run(){
System.out.println("・ ・ ・");
}
}
Recommended Posts