설계라는 특수한 목적의 클래스
개발시 필요한 설계 가이드 라인
interface 인터페이스명 {
추상 메서드 정의..
}
public abstract는 컴파일러가 자동으로 추가해주기 떄문에 생략가능
public interface Calculator {
int add(int num1, int num2); // public abstract 자동 추가
}
implements : 구현하다
class 클래스명 implements 인터페이스{
}
public interface Calculator {
int add(int num1, int num2); // public abstract 자동 추가
}
- - - - - - - - - - - - - - - - - - - - - - -
public class SimpleCalculator implements Calculator {
public int add(int num1, int num2){
return num1 + num2;
}
}
여러 인터페이스도 가능
class 클래스명 implements 인터페이스1, 인터페이스2,...{
}
public interface Buyer {
void sell();
}
public interface Seller {
void buy();
}
public class Order implements Buyer, Seller{
@Override
public void sell() {
System.out.println("구매!");
}
@Override
public void buy() {
System.out.println("판매");
}
}
public class Ex01 {
public static void main(String[] args) {
Order od = new Order();
od.buy();
od.sell();
}
}
인터페이스에서 변수를 선언하면 자동으로 정적 상수로 선언된다.
인터페이스는 설계를 위하기 떄문에, 객체를 생성해서 사용할수있는 인스턴스변수를 선언할수없다. 그렇기 떄문에 객체를 생성하지 않고도 사용할수 있는 정적상수(static)로 선언되어진다.
(컴파일러가public static final를 자동으로 추가)public interface Calculator{ int NUM = 10 // public static final 자동추가 ----------------------------------------- public class Ex01 { public static void main(String[] args) { System.out.println(Calculator.NUM); } }
동일한 메서드를 구현해야하는데 하위클래스가 100개가 있다면, 하위클래스 100개 모두 정의해야한다는 문제가있다. 이를 해결하기 위해 디폴트 메서드가 생겨남.
Adapter클래스 기본적으로 모두 정의 필요한것만 재정의하면된다 → 그렇지만 클래스의 약점인 다중상속불가로 인해 다중상속이 가능한 인터페이스에서도 사용하기위해 생겨남
public interface Buyer {
void buy();
default void order(){ //디폴트 메서드 (추상이 아닌 완전히 구현된 메서드)
System.out.println("바이어에서 주문");
}
}
-------------------------------------------
public class Ex02 {
public static void main(String[] args) {
Order order = new Order();
order.order(); // Buyer 인터페이스에서 구현된 디폴트메서드
}
}
----------------------------------------------
public class Order implements Buyer, Seller{
//디폴트메서드로 정의했기떄문에 재정의 등 아무것도안해도됨.
}
public interface Seller {
public static void staticMothod(){
System.out.println("정적메서드");
}
}
- - - - - - - - - - - - - - - - - -
public class Ex02 {
public static void main(String[] args) {
Seller.staticMothod(); //정적메서드 호출
}
}
public interface A {
void method();
}
public interface B {
void method();
}
public class C implements A, B{
@Override
public void method() {
System.out.println("C에서구현한 메서드");
}
}
public class Ex01{
public static void main(String[] args) {
C c = new C();
c.method(); //>C에서구현한 메서드
}
}
인터페이스: 추상 메서드이기 때문에 호출 주체는 명확(구현환 클래스의 메스드) : 다중 상속 가능클래스: 다중 상속시 인스턴스 메서드가 동일하면 하위클래스가 호출 주체를 정하지 못한다. : 다중상속불가능
클래스에서 다중클래스를 상속못하는 이유
하위클래스에 동일한이름의 메서드가 있을 경우
메인함수에서 상위클래스 객체를 생성하고 메서드를 호출하면 오버라이딩된 하위클래스의 메서드가 호출되고 어떤 하위클래스의 메서드를 사용할지 모르기 떄문에.
이름이 중복된 메서드중 사용하고싶은 메서드의 인터페이스명을 제시해야한다.
public interface Seller {
default void order(){
System.out.println("셀러에서 주문");
}
}
---------------------------------
public interface Buyer {
default void order(){ //디폴트 메서드
System.out.println("바이어에서 주문");
}
}
----------------------------------
public class Order implements Buyer, Seller{
public void order(){
Buyer.super.order(); //이름이 중복된 메서드의 인터페이스명을 제시해야한다.
}
}
public interface X {
void method1();
}
-----------------------------
public interface Y {
void method1();
}
-----------------------------
public interface Z extends X,Y {
void mothod3();
}
----------------------------
public class Ex01 implements Z {
@Override
public void method1() { //중복된 메서드
System.out.println("Ex01"); // 출력됨.
}
}
다른 인터페이스에 이름이 같은 메서드가 있을지라도,
하위 클래스에 메서드가 실행(오버라이딩)
Collection : 데이터 군집 : 자료Framework : 표준적인 설계 틀collection 인터페이스
- List 인터페이스
- Set 인터페이스
Map 인터페이스