티스토리 뷰
출처
howtodoinjava.com/java9/java9-private-interface-methods/
요약
자바 버전별 인터페이스에 선언 및 구현 할 수 있는 메소드
java7, java8 이라는 명칭은 안 맞는다. (그 당시는 1.7,1.8 이었으니)
개인의견이지만 interface 와 abstract class 간 구분이 없어지는 것 같아서 조금 그렇다.
public abstract | N/A |
public default | java8 이상 |
public static | java8 이상 |
private | java9 이상 |
private static | java9 이상 |
java 7 이전
package before_java7;
public interface SomeInterface {
//public abstract 는 생략 가능
public abstract void someMethod();
}
package before_java7;
public class BeforeJava7 implements SomeInterface {
@Override
public void someMethod() {
System.out.println("override some method(public abstract)");
}
public static void main(String args[]) {
SomeInterface instance = new BeforeJava7();
instance.someMethod();
}
}
override some method(public abstract)
java 8
package java8;
public interface SomeInterface {
//public abstract 는 생략 가능
public abstract void someMethod();
public default void someMethod2() {
System.out.println("public default method");
}
public static void someMethod3() {
System.out.println("public static method");
}
}
package java8;
public class Java8 implements SomeInterface {
@Override
public void someMethod() {
System.out.println("override some method(public abstract)");
}
public static void main(String args[]) {
SomeInterface instance = new Java8();
instance.someMethod();
instance.someMethod2();
SomeInterface.someMethod3();
}
}
override some method(public abstract)
public default method
public static method
java 9
package after_java9;
public interface SomeInterface {
//public abstract 는 생략 가능
public abstract void someMethod();
public default void someMethod2() {
System.out.println("call private (default) method");
someMethod4();
System.out.println("call private static method");
someMethod5();
System.out.println("public default method");
}
public static void someMethod3() {
System.out.println("public static method");
}
//default 인 것 같은데 선언은 안된다.
private void someMethod4(){
System.out.println("private method");
}
private static void someMethod5() {
System.out.println("private static method");
}
}
package after_java9;
public class Java9 implements SomeInterface {
@Override
public void someMethod() {
System.out.println("override some method(public abstract)");
}
public static void main(String args[]) {
SomeInterface instance = new Java9();
instance.someMethod();
instance.someMethod2();
SomeInterface.someMethod3();
}
}
override some method(public abstract)
call private (default) method
private method
call private static method
private static method
public default method
public static method
기타
java 는 다중상속을 제한하는게 기본 방침일텐데 이런 식이면 의미가 없어지지 않나 해서 추가 테스트
컴파일 타임에서 강제한다.
메소드 이름이 같으니 (시그네쳐도 같긴한데) override 하라고 강제
package after_java9;
public interface SomeInterface1 {
//public abstract 는 생략 가능
public abstract void someMethod();
public default void someMethod2() {
System.out.println("SomeInterface1: call private (default) method");
someMethod4();
System.out.println("SomeInterface1: call private static method");
someMethod5();
System.out.println("SomeInterface1: public default method");
}
public static void someMethod3() {
System.out.println("SomeInterface1: public static method");
}
//default 인 것 같은데 선언은 안된다.
private void someMethod4(){
System.out.println("SomeInterface1: private method");
}
private static void someMethod5() {
System.out.println("SomeInterface1: private static method");
}
}
package after_java9;
public interface SomeInterface2 {
//public abstract 는 생략 가능
public abstract void someMethod();
public default void someMethod2() {
System.out.println("SomeInterface2: call private (default) method");
someMethod4();
System.out.println("SomeInterface2: call private static method");
someMethod5();
System.out.println("SomeInterface2: public default method");
}
public static void someMethod3() {
System.out.println("SomeInterface2: public static method");
}
//default 인 것 같은데 선언은 안된다.
private void someMethod4(){
System.out.println("SomeInterface2: private method");
}
private static void someMethod5() {
System.out.println("SomeInterface2: private static method");
}
}
package after_java9;
public class Java9 implements SomeInterface1, SomeInterface2 {
@Override
public void someMethod() {
System.out.println("override some method(public abstract)");
}
// default method 이름이 충돌나니 override 를 필수로 하게 하며 안하면 에러를 뱉는다
//java: types after_java9.SomeInterface1 and after_java9.SomeInterface2 are incompatible;
// class after_java9.Java9 inherits unrelated defaults for someMethod2() from types after_java9.SomeInterface1 and after_java9.SomeInterface2
@Override
public void someMethod2() {
System.out.println("override some method2");
}
public static void main(String args[]) {
System.out.println("=====INTERFACE1=====");
SomeInterface1 instance1 = new Java9();
instance1.someMethod();
instance1.someMethod2();
SomeInterface1.someMethod3();
System.out.println("=====INTERFACE1=====");
System.out.println();
System.out.println("=====INTERFACE2=====");
SomeInterface2 instance2 = new Java9();
instance2.someMethod();
instance2.someMethod2();
SomeInterface2.someMethod3();
System.out.println("=====INTERFACE2=====");
System.out.println();
System.out.println("=====CLASS=====");
Java9 java9 = new Java9();
java9.someMethod();
java9.someMethod2();
System.out.println("=====CLASS=====");
}
}
=====INTERFACE1=====
override some method(public abstract)
override some method2
SomeInterface1: public static method
=====INTERFACE1=====
=====INTERFACE2=====
override some method(public abstract)
override some method2
SomeInterface2: public static method
=====INTERFACE2=====
=====CLASS=====
override some method(public abstract)
override some method2
=====CLASS=====
'개발관련 > java' 카테고리의 다른 글
PECS - java generic (0) | 2021.05.08 |
---|---|
JVM Compressed OOPS (0) | 2021.03.29 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 기술센싱
- WebSocket
- 말의품격
- opensearch
- 전설로떠나는월가의영웅
- 클린 아키텍처
- 에픽테토스
- percolate
- Dangling
- completablefuture
- meta character
- pecs
- 사기꾼증후군
- fsync
- Generic
- elasticsearch
- COMMIT
- PatternSyntaxException
- Async
- flush
- 기술블로그
- jhipster
- AWS
- 기술사이트
- 만들면서 배우는 클린 아키텍처
- Kafka
- Java
- kafka 2.8.0
- Spring
- 개발자
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함