interface 인터페이스
서비스 요청에 따른 중계자 역할을 하는 것과 같다.
interface, port를 의미한다. 서버와 유저를 연결시켜주는 용도.
인터페이스(interface)의 내부 규칙
- 인터페이스는 호출 규칙을 정의하는 것이기 때문에 추상 메서드만 선언 가능하다.
- 인터페이스는 인스턴스 변수를 가질 수 없다.
즉 인터페이스에 선언된 모든 메서드는 public이고 abstract이다.
또한 인터페이스에 선언된 모든 필드(영역)는 public이고 static이며 final이다.
- 인터페이스에는 상수와 추상 메서드만 존재한다.
- 추상 클래스처럼 선언만 하고, 이 추상 인터페이스를 상속받는 자식은 implements를 붙여 사용한다.
https://limkydev.tistory.com/197
개념 정리.
interface A_Inter { // 부모 인터페이스 클래스 선언
final int CONST =100; // 상수
abstract int getA(); // 추상 메서드
}
class B_Inter implements A_Inter { // 부모 인터페이스 상속받은 자식 클래스
public int getA() { // Method Overriding
return CONST;
}
}
public class ex1 {
public static void main(String[] args) {
B_Inter bi = new B_Inter();
// 자식 클래스 객체 생성, 부모 변수와 자식 메서드 모두 사용 가능
System.out.println(bi.getA());
}
}
Interface 상속 및 다형성
interface Inter_Menu1 {
String food1();
// final, abstract, static 은 디폴트이며, 생략 가능하다. 없을 수 없다.
abstract String food2();
}
interface Inter_Menu2 {
abstract String food3();
}
interface Inter_Menu3 extends Inter_Menu1, Inter_Menu2 {
// interface는 다중 상속 가능하며, 같은 interface는 implements 대신 extends를 사용한다.
abstract String food4();
// food1(), food2(), food3(), food4() 를 모두 가졌다.
}
public class ex1 implements Inter_Menu3 {
// ex1 class는 Inter_Menu1, Inter_Menu2, 그리고 INter_Menu3을 모두 가지고 있음.
public String food1() {
return "짜장면";
}
public String food2() {
return "짬뽕";
}
public String food3() {
return "군만두";
}
public String food4() {
return "뭘까용";
}
public static void main(String[] args) {
ex1 e = new ex1();
System.out.println("Menu");
Inter_Menu1 im1 = e;
Inter_Menu2 im2 = e;
Inter_Menu3 im3 = e;
System.out.println(e.food1());
System.out.println(e.food2());
System.out.println(e.food3());
System.out.println(e.food4());
}
}
interface get()함수 호출
interface Int1 {
final int A = 100;
abstract int getA();
abstract void setData();
}
interface Int2 {
final String B = "Paul";
abstract String getB(String a);
// 인자를 넣어 메서드 호출
}
class IntTest implements Int1, Int2 {
public int getA() {
return A;
}
public String getB(String a) {
return B + a;
}
public void setData() {}
// 위에서 이미 선언되어져 있으므로, 무조건 구현해야한다.
}
class df{
public static void main(String[] args) {
IntTest it = new IntTest();
System.out.println(it.getB("dd"));
System.out.println(it.getA());
}
}
JAVA 내부 클래스 (0) | 2022.07.29 |
---|---|
JAVA enum 열거형 (0) | 2022.07.29 |
JAVA 추상 메서드와 클래스 Abstract method, class (0) | 2022.07.28 |
JAVA Generics Class Method 제네릭 클래스 메서드, 배열 Array (0) | 2022.07.28 |
JAVA Object class 최상위 클래스 객체 (0) | 2022.07.28 |