Java/Java

[Java] 인터페이스(interface)

기록하는_사람 2022. 10. 15. 12:43

인터페이스(interface)

📌 인터페이스(interface)

: 추상 메서드로 이루어진 것.

  인터페이스를 대상으로 인스턴스 생성이 불가능하며, 다른 클래스에 의해 상속 가능함.

 

💡  추상 메소드(Abstract Methods)

: 메소드의 몸체가 비어 있는 메소드. 

 

📌 인터페이스에 존재할 수 있는 메소드

① 추상 메소드

② 디폴트 메소드

③ static 메소드

: 인터페이스의 모든 메소드는 public으로 간주함

 

📌 인터페이스 내 변수

① 선언과 동시에 초기화해야 함.

 public, static, final로 간주. (상수)

 

📌 구현(Implementation)

: 클래스가 인터페이스를 상속하는 행위.

 

📌 extends와  implements

 class - class 상속 : extends

 interface - interface 상속 : extends

 interface - calss 구현 : implements

 

📌 한 클래스에 둘 이상의 인터페이스 동시 구현 가능.

      상속과 구현 동시 사용 가능. 

 

📌 인터페이스의 형을 대상으로 참조 변수의 선언 가능.

      인터페이스의 추상  메소드와 이를 구현하는 메소드 사이에 오버라이딩 관계 성립.

      (어노테이션 @Override 선언 가능.) 

      참조 변수를 대상으로 인터페이스의 메소드 호출 가능.

 

📌 인터페이스를 구현하는 클래스는 인스턴스에 존재하는 모든 추상 메소드를 구현해야 함. 

      구현하지 않으면, 해당 클래스를 대상으로 하는 인스턴스 생성 불가. 

 

📌 interfaceof 연산

: a가 참조하는 인스턴스가

  b의 인스턴스이거나 b를 상속하는 클래스의 인스턴스인 경우, true 반환.

if(a instanceof b)

 

인터페이스(interface) - 추상 메소드(Abstract Methods)

📌 추상 메소드(Abstract Methods)

: 메소드의 몸체가 비어 있는 메소드. 

interface Printable {
	public void print(String doc);  // 추상 메소드.
}
class Printer implements Printable {
	public void print(String doc) {  // Printable 인터페이스의 print 메소드 구현.
		System.out.println(doc);
    }
}

 

인터페이스(interface) - 디폴트 메소드(Default Methods)

📌 디폴트 메소드(Default Methods)

: 인터페이스에 추상 메소드를 추가해야 하는 상황에서 이전에 개발해 놓은 토드레 영향을 주지 않기 위해 등장한 메소드.

 

📌 자체로 완전한 메소드.

      이를 구현하는 클래스가 오버라이딩 하지 않아도 됨. 

 

인터페이스(interface) - static 메소드

📌 static 메소드

: public으로 간주하며, 호출 방법은 클래스의 static 메소드 호출 방법과 동일함. 

 

마커 인터페이스(Maker Interface)

📌 마커 인터페이스(Maker Interface)

: 클래스에 특별한 표식을 다는 메소드. 

  아무런 메소드가 존재하지 않는 경우가 많음. 

interface Upper {}  // 마커 인터페이스.

 

인터페이스(Interface) - 예시

📄 Printable.java

package prac_interface;

interface Color{}

public interface Printable {
	static void printLine(String s) {  // static 메소드.
		System.out.println("==================");
		System.out.println(s);
		System.out.println("==================");
	}
	
	void print(String s);  // 추상 메소드. 
	
	default void printColor(String s) {};  // 디폴트 메소드.
	
	default void printStartLine(String s) {  // 디폴트 메소드.
		printLine(s);  // static 메소드 호출.
	};
}

📄 APrinter.java

package prac_interface;

public class APrinter implements Printable {
	@Override
	public void print(String s) {
		System.out.println("A Printer : " + s);
	}
}

📄 BPrinter.java

package prac_interface;

public class BPrinter implements Printable {
	@Override
	public void print(String s) {
		System.out.println("B Printer - Black and White ver : " + s);
	}
	
	@Override
	public void printColor(String s) {
		System.out.println("B Printer - Color ver : " + s);
	}
}

📄 PrinterMain.java

package prac_interface;

public class BPrinter implements Printable {
	@Override
	public void print(String s) {
		System.out.println("B Printer - Black and White ver : " + s);
	}
	
	@Override
	public void printColor(String s) {
		System.out.println("B Printer - Color ver : " + s);
	}
}

// ==================
// A Printer
// ==================
// A Printer : Hello! Nice to meet you
//
// ==================
// B Printer
// ==================
// B Printer - Black and White ver : Hello! Nice to meet you
// B Printer - Color ver : Hello! Nice to meet you
//
// ==================
// the end
// ==================