interface

이규은·2021년 10월 8일
0

인터페이스

목록 보기
1/1

interface란?

자바에서는 인터페이스라는 것을 통해 다중 상속을 지원하고 있다.
인터페이스란 다른 클래스를 작성할 때 기본이 되는 툴을 제공하며 다른 클래스 사이의 중간 매개 역할을 담당하는 일종의 추상 클래스이다.

인터페이스는 추상 메소드와 상수만을 포함할 수 있다.

interface선언

접근제어자 interface 인터페이스이름 {
	public static final 타입 상수이름 =;
    
    public abstract 메소드이름(매개변수목록);
}

위와 같이 interface를 선언할 때는 접근제어자와 함께 interface 키워드를 사용하면 된다.

인터페이스의 모든 필드는 public static final이어야 하며
모든 메소드는 public abstract 이어야 한다.
이 부분은 모든 인터페이스에 공통으로 적용되기 때문에 생략 가능하다.

interface 구현

인터페이스는 자신이 직접 인스턴스를 생성할 수 없다. 따라서 추상 메소드를 구현해 줄 클래스를 작성 해야 한다.

class 클래스이름 implements 인터페이스이름 {
}

위와 같이 구현한다.

interface animal {
    public abstract void cry();
}

interface eating {
    public abstract void food();
}

class cat implements animal, eating {
    public void cry() {
        System.out.println("냐옹");
    }

    public void food() {
        System.out.println("난 물고기가 좋아");
    }
}

class dog implements animal, eating {
    public void cry() {
        System.out.println("멍멍");
    }

    public void food() {
        System.out.println("난 고기가 좋아");
    }
}

public class InterfaceStduy {
    public static void main(String[] args) {
        cat cat = new cat();
        dog dog = new dog();
        
        cat.cry();
        cat.food();
        dog.cry();
        dog.food();
    }
}

위와 같이 다중 상속을 할 수 있다.

interface 레퍼런스를 통해 구현체를 사용하는 방법

interface animal {
    public abstract void cry();
}

interface eating {
    public abstract void food();
}

class cat implements animal, eating {
    public void cry() {
        System.out.println("냐옹");
    }

    public void food() {
        System.out.println("난 물고기가 좋아");
    }
}

class dog implements animal, eating {
    public void cry() {
        System.out.println("멍멍");
    }

    public void food() {
        System.out.println("난 고기가 좋아");
    }
}

public class InterfaceStduy {
    public static void main(String[] args) {
        animal AnimalCat = new cat();
        eating EationCat = new cat();

        AnimalCat.cry();
        EationCat.food();

        //AnimalCat.food();	에러
        //EationCat.cry();	에러
    }
}

인터페이스의 레퍼른스를 사용하면 해당 인터페이스에서 정의된 메서드만을 사용할 수 있다.

inertface 상속

interface creature {
    public abstract void breathing();
}

interface animal extends creature {
    public abstract void cry();
}

interface eating {
    public abstract void food();
}

class cat implements animal {
    public void cry() {
        System.out.println("냐옹");
    }

    public void food() {
        System.out.println("난 물고기가 좋아");
    }

    public void breathing() {
        System.out.println("숨쉬는중!");
    }
}

class dog implements animal, eating {
    public void cry() {
        System.out.println("멍멍");
    }

    public void food() {
        System.out.println("난 고기가 좋아");
    }

    public void breathing() {
        System.out.println("숨쉬는중!");
    }
}

public class InterfaceStduy {
    public static void main(String[] args) {
        cat cat = new cat();
        dog dog = new dog();

        cat.cry();
        cat.breathing();
        dog.cry();
        dog.breathing();
    }
}

extends를 사용하여 인터페이스끼리 상속이 가능하다.
인터페이스는 구현체가 없어 다중상속을 할 때 문제가 발생하지 않는다.

java8 에서 추가된 기능

  1. default 메서드
    참조 변수로 함수를 호출할 수 있다.
    implements 한 클래스에서 재정의가 가능하다.

  2. static 메서드
    반드시 클래스 명으로 메서드를 호출해야 한다.
    재정의가 불가능하다.

인터페이스의 기본 메소드

interface animal {
    public abstract void cry();
}

interface eating {
    public abstract void food();

    default void eat() {
        System.out.println("냠냠");
    }
}

class cat implements animal, eating {
    public void cry() {
        System.out.println("냐옹");
    }

    public void food() {
        System.out.println("난 물고기가 좋아");
    }

    public void eat() {
        eating.super.eat();
    }
}


class dog implements animal, eating {
    public void cry() {
        System.out.println("멍멍");
    }

    public void food() {
        System.out.println("난 고기가 좋아");
    }
}

public class InterfaceStduy {
    public static void main(String[] args) {
        cat cat = new cat();
        dog dog = new dog();

        cat.cry();
        cat.eat();

    }
}

default 키워드를 사용하여 기본 메서드를 구현할수 있다
기본 메서드를 선언했다면 구현해야 한다
기본 메스드도 오버라이딩 할 수 있다.

인터페이스의 static 메소드

interface A {
    static void hello() {
        System.out.println("Hello!");
    }
}

public class InterfaceStduy {
    public static void main(String[] args) {
        A.hello();
    }
}

인터페이스 내부의 static 메서드를 추가할 수 있다.
기존의 static 메서드와 같이 인스턴스를 생성하지 않고 사용할 수 있다.

인터페이스의 private 메소드

자바9 버전 부터는 private와 private static 을 사용할 수 있다.

자바 9는 private 접근 제어자를 인터페이스 내부에서 사용할 수 있도록 했다.

profile
안녕하세요

0개의 댓글