[JAVA] 인터페이스 (interface)

[JAVA] 인터페이스 (interface)

개요

모든 기능을 추상화로 정의만 하고 구현은 하지 않은 것이다.

미리 인터페이스를 작성함으로써 메소드를 정할 수 있다.

클래스간 결합도를 낮춘 유연한 방식의 프로그래밍이 가능해진다.

다중 상속을 구현하기 위해서 사용한다.

Realization 관계 (인터페이스의 정의만 있는 메소드를 오버라이딩하여 실제 기능으로 구현하는 관계)

문법

public interface Command { public [static final] int MEMBER = 5; public [abstract] String useCommand (int num) throws Exception; }

interface 타입으로 선언한다.

그 안의 요소는 추상메소드와 변수만이 존재한다.

public interface ActionCommand extends Command throws Exception{ }

interface는 다른 interface에게 상속할 수 있다.

public String action(int no) implements Command throws Exception{ public int MEMBER = 5; public String useCommand (int no) throws Exception{ String power = call(no); return power; } }

action 클래스에서 Command 인터페이스 구현을 한다는 의미로 'implements Command'를 작성한다.

구현할 class에서는 무조건 인터페이스의 요소를 사용하여야 한다. 이 때 메소드는 추상 메소드가 아닌 구상 메소드가 된다.

from http://jollypyun.tistory.com/81 by ccl(A) rewrite - 2021-10-31 20:27:33