Interface

Interface Declaration, Inheritance, Implementation

-In Java programming aspect, interface is a group of abstract methods. It also includes static method, inheritance, default methods.

-Interface connects objects. Java를 활용한 Spring을 배울 때 인터페이스에 대해 혼동하지 말자.

-There is nothing implemented in interface.

-All of members are public.

Encapsulation : t.hour(x) . t.getHour(o)

  • Difference between interface and abstract class

  1. Abstract class : The class that has abstract methods. It has not only abstract methods, but also constructor and iv(instance variable/instance method).

  2. Interface : The one that only have abstract methods. It cannot have any other things.

  • Interface Declaration

interface (name) {
    public static final (type) (name) = (value);
    public abstract (method_name) (parameter_list);
}
interface PlayingCard {
    // some parts can be skipped. 
    public static final int SPADE = 4;
    final int DIAMOND = 3;//public static final int DIAMOND = 3;
    static int HEART = 2;//public static final int HEART = 2;
    int CLOVER = 1;//public static final int CLOVER = 1;
    
    public abstract String getCardNumber();
    String getCardKind();//public abstract String getCardKind();
}

interface CANNOT have variable such as iv, cv...

interface ALWAYS have public static final.(no exception, always constant.)

interface ALWAYS have public abstract methods.

  • Interface Inheritance

-Interface parent can be only interface.(Object is not the root.)

-Multiple inheritance is possible : Java is single inheritance because conflict between same name from different classes and different implementation. But when it comes to abstract method, even though it is same name, it has no implemented part so it does not matter.

-It is same as implementing abstract class by inheritance using "extends". The only difference is using keyword "implements"

-Implement(Complete) abstract method.

interface Fightable {
    
    (public abstract) void move(int x, int y);
    (public abstract) void attack(Unit u);
}
//Fight class implements Fightable interface
//"abstract" does not exist in front of Fighter class.
class Fighter implements Fightable {
    //implement abstract method
    public void move(int x, int y) {..}
    public void attack(Unit u) {..}
}
//implement only some parts then add "abstract"
abstract class Fighter implements Fightable {
    public void move(int x, int y)
}

Last updated