티스토리 뷰
Java version 8에서 interface에 default 메소드와 static 메소드가 사용가능하게 되었다고 합니다.
소스를 통해 설명드리겠습니다.
1. Calculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Calculator { | |
public int plus(int pre, int post); | |
public int multi(int pre, int post); | |
default int execPlus(int pre, int post){ | |
return pre + post; | |
} | |
static int execMulti(int pre, int post){ | |
return pre * post; | |
} | |
} |
2, 3번째 줄 : 일반 메소드 선언 (추상메소드의 역할 abstract가 생략되어있다.)
5번째 줄 : default 메소드 선언
9번째 줄 : static 메소드 선언
2. CalculatorImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CalculatorImpl implements Calculator { | |
@Override | |
public int plus(int pre, int post) { | |
return pre + post; | |
} | |
@Override | |
public int multi(int pre, int post) { | |
return pre * post; | |
} | |
} |
CalculaterImpl class는 Calculater를 implements하여 구현한 클래스입니다.
* default 메소드도 @Override로 재정의 할수 있다.
* static메소드는 재정의 불가능!
3. InterfaceEx.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class InterfaceEx { | |
public static void main(String[] args) { | |
Calculator cal = new CalculatorImpl(); | |
int resultPlus = cal.plus(3, 9); | |
int resultMulti = cal.multi(3, 9); | |
System.out.println("resultPlus 값은 : " + resultPlus); | |
System.out.println("resultMulti 값은 : " + resultMulti); | |
int resultExecPlus = cal.execPlus(3, 9); | |
System.out.println("dafault method 호출 결과 : " + resultExecPlus); | |
int resultExecMulti = Calculator.execMulti(3, 9); | |
System.out.println("static method 호출 결과 : " + resultExecMulti); | |
} | |
} |
5 ~ 8번째 줄 : 일반 메소드를 호출한 부분.
10 ~ 11번째 줄 : cal이라는 참조변수를 이용해 호출가능. (재정의 했을 경우 재정의한 메소드가 호출된다.)
13 ~14번째 줄 : 클래스명.static메소드(); 로만 호출이 가능하다!
* 참조변수.static메소드()를 호출하면 에러가 발생한다.
[ 정리 ]
interface의 default 메소드
- interface에서도 메소드 구현이 가능하다.
- 참조 변수로 함수를 호출할 수 있다.
- implements한 클래스에서 재정의가 가능하다.
interface의 static 메소드
- interface에서 메소드 구현이 가능하다.
- 반드시 클래스 명으로 메소드를 호출해야 한다.
- 재정의 불가능!
'programming > JAVA와 maven' 카테고리의 다른 글
[JAVA] 익명클래스 (0) | 2017.04.06 |
---|---|
[JAVA] 내부클래스 (0) | 2017.04.05 |
Maven이란? (0) | 2017.02.11 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 익명중첩클래스
- 알고리즘문제해결전략
- 연애가끝났다
- Eclipse
- jsp
- aoj
- 알고스팟
- 지역중첩클래스
- 시스템 카탈로그
- 요나스요나슨
- smarteditor2
- url mapping
- 익명클래스
- 중첩클래스
- 백준
- 비식별
- 킬러안데르스와그의친구둘
- 클러스터인덱스
- 데이터베이스 문제
- 백준저지
- servlet
- Spring
- 낭만적연애와그후의일상
- 인스턴스클래스
- boj
- 넌클러스터인덱스
- java
- static클래스
- tcp/ip monitor
- 알고리즘
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함