java (interface)

Conditions that can be used as an interface

** Abstract class master ball (top) ** ① All methods are abstract methods ② Basically it does not have a field

Highly abstract Human class

Human.java


public abstract class Human {     //Meet ①② above
  public abstract void run();
}

Human class declared as an interface

Human.java


public interface Human {
  void run();                        //public abstract can be omitted
}

Interface implementation

Hero.java


public class Hero implements Human { 
}

Multiple inheritance by interface

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

Interface inheritance

Hero.java


public interface Human extend Creature {
  void talk();
  void watch();
  void hear();
  //In addition, run from the parent interface()Inheriting
}

Use implements and extend properly

----- ʻextend (inheritance) between classes and interfaces --If you use an interface in a class (heterogeneous), ʻimplements

Defining classes using both implements and extend

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

java (interface)
[java] interface
About Java interface
[Java] About interface
[Java] Functional interface
About interface, java interface
About Java functional interface
Callable Interface in Java
java standard functional interface
interface
JAVA learning history interface
Java
Java learning memo (interface)
Java
Advanced inheritance abstract, interface -java
JAVA learning history interface inheritance
Check Java9 Interface private methods
Access the network interface in Java
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
Java array
Studying Java ―― 9
Java scratch scratch
java (constructor)
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
java (array)
Java static
java beginner 4
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
[Java beginner] About abstraction and interface
Interface benefits
java reflexes
Java memorandum
☾ Java / Collection
new interface
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance