인터페이스는
static final 필드
public 추상 메소드 (몸통이 없는 instance 메소드)
public default 메소드 (몸통이 있는 instance 메소드)
private 메소드(몸통이 있는 instace 메소드)
static public 메소드
static private 메소드
생성자x
또한 여러 인터페이스를 implements 가능하다(관련 내용은 아래에서)
인터페이스 멤버 변수는 public static final 로만 지정가능하며 생략 가능합니다 .
또한 멤버 함수는 public abstract final 로만 선언 가능합니다.
실제 변수를 int value = 0; 으로 선언하는 경우 public static final가 생략되어 있는 형태입니다.
상위 클래스에서 default 메소드 활용해 공통적인 기능은 만들어서 사용가능
interface MyInterface14 {
//필드: public static final
//인스턴스 메소드 :
//추상메소드
//default 메소드
//private 메소드
//static(정적) 메소드
//public static 메소드
//private static 메소드
//사용가능
}
interface안에 추상메소드를 구현하려면 상속은 extends지만 interface는 implements 사용
implemetns 한 경우 interface 안에 있는 모든 추상 메소드를 구현(재정의 @Override)를 해야 한다.
왜냐면 모두 구현(재정의) 안하면 추상 클래스로 바뀌어 버리니깐
extends 경우는 필요한 것만 골라서 재정의
다중 인터페이스
extens 같은 경우 하나의 클래스만 가능하지만 interface 같은 경우 여러 개의 interface를
implements 할 수 있다
public class C03interface {
public static void main(String[] args) {
MyClass03 o1 = new MyClass03();
o1.method1();
o1.method2();
MyInterface031 i1 = o1;
MyInterface032 i2 = o1;
System.out.println(System.identityHashCode(o1));
System.out.println(System.identityHashCode(i1));
System.out.println(System.identityHashCode(i2));
i1.method1();
// i1.method2();
// i2.method1();
i2.method2();
}
}
interface MyInterface031 {
void method1();
}
interface MyInterface032 {
void method2();
}
// 여러 인터페이스를 구현할 수 있다.
class MyClass03 implements MyInterface031, MyInterface032 {
//MyInterface031 의 추상메소드
@Override
public void method1() {
System.out.println("MyClass03.method1");
}
//MyInterface032 의 추상메소드
@Override
public void method2() {
System.out.println("MyClass03.method2");
}
}
반응형
'JAVA > 기본' 카테고리의 다른 글
제네릭스 <> (Generic) (0) | 2023.09.07 |
---|---|
람다식 (lambda expression) (0) | 2023.09.04 |
추상 클래스 (abstract class) / 추상 메소드 (abstract method) (0) | 2023.08.30 |
상속 Extends / 오버라이딩(overriding) / 오버로딩(Overloading) (0) | 2023.08.23 |
생성자(Constructor) (0) | 2023.08.22 |