[JAVA] 객체 지향 핵심 – 인터페이스의 여러가지 요소와 여러 인터페이스 구현하기, 인터페이스의 상속

– 상수

  • 모든 변수는 상수로 변환 됨 public static final

– 추상 메서드

  • 모든 선언된 메서드는 추상 메서드 public absrtact

– 디폴트 메서드 (자바 8이후)

  • 구현을 가지는 메서드, 인터페이스를 구현하는 클래스들에서 공통으로 사용할 수 있는 기본 메서드
  • dafault 키워드 사용
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
default void description() {
System.out.println("정수 계산기를 구현합니다.");
myMethod();
}
default void description() { System.out.println("정수 계산기를 구현합니다."); myMethod(); }
default void description() {
	System.out.println("정수 계산기를 구현합니다.");
	myMethod();
}
  • 구현 하는 클래스에서 재정의 할 수 있음
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@Override
public void description() {
System.out.println("CompleteCalc에서 재정의한 default 메서드");
//super.description();
}
@Override public void description() { System.out.println("CompleteCalc에서 재정의한 default 메서드"); //super.description(); }
@Override
public void description() {
	System.out.println("CompleteCalc에서 재정의한 default 메서드");
	//super.description();
}
  • 인터페이스를 구현한 클래스의 인스턴스가 생성 되어야 사용 가능함

– 정적 메서드(자바8이후)

  • 인스턴스 생성과 상관 없이 인터페이스 타입으로 사용할 수 있는 메서드
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
static int total(int[] arr) {
int total = 0;
for(int i: arr) {
total += i;
}
mystaticMethod();
return total;
}
static int total(int[] arr) { int total = 0; for(int i: arr) { total += i; } mystaticMethod(); return total; }
static int total(int[] arr) {
	int total = 0;
		
	for(int i: arr) {
		total += i;
	}
	mystaticMethod();
	return total;
}

– private 메서드(자바9이후)

  • 인터페이스를 구현한 클래스 에서 사용하거나 재정의 할 수 없음
  • 인터페이스 내부에서만 사용하기 위해 구현하는 메서드
  • default 메서드나 static 메서드에서 사용함
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
private void myMethod() {
System.out.println("private method");
}
private static void mystaticMethod() {
System.out.println("private static method");
}
private void myMethod() { System.out.println("private method"); } private static void mystaticMethod() { System.out.println("private static method"); }
private void myMethod() {
	System.out.println("private method");
}
	
private static void mystaticMethod() {
	System.out.println("private static method");
}

– 여러 인터페이스 구현

  • 자바의 인터페이스는 구현 코드가 없으므로 하나의 클래스가 여러 인터페이스를 구현 할 수 있음
  • 디폴트 메서드가 중복 되는 경우는 구현 하는 클래스에서 재정의 하여야 함
  • 여러 인터페이스를 구현하는 클래스는 인터페이스 타입으로 형 변환되는 경우 해당 인터페이스에 선언된 메서드만 사용 가능 함

– 디폴트 메서드가 중복 되는 경우

  • 구현 코드를 가지고 인스턴스 생성된 경우만 호출되는 디폴트 메서드의 경우 두 개의 인터페이스에서 중복되면 구현하는 클래스에서 반드시 재정의를 해야 함

– 인터페이스의 상속

  • 인터페이스 사이에도 상속을 사용할 수 있음
  • extends 키워드를 사용
  • 인터페이스는 다중 상속이 가능하고 구현 코드의 상속이 아니므로 타입 상속 이라고 함
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public interface Sell {
void sell();
default void order(){
System.out.println("Seller Order");
}
}
public interface Buy {
void buy();
default void order(){
System.out.println("Buyer Order");
}
}
public class Customer implements Buy,Sell{
@Override
public void sell() {
System.out.println("Customer sell");
}
@Override
public void buy() {
System.out.println("Customer Buy");
}
@Override
public void order() {
System.out.println("Customer Order");
}
public void sayHello(){
System.out.println("Say Hello");
}
}
public class CustomerTest {
public static void main(String[] args) {
Customer customer = new Customer();
customer.sell();
customer.buy();
customer.sayHello();
Buy buyer = customer;
Sell seller = customer;
seller.sell();
customer.order();
}
}
public interface Sell { void sell(); default void order(){ System.out.println("Seller Order"); } } public interface Buy { void buy(); default void order(){ System.out.println("Buyer Order"); } } public class Customer implements Buy,Sell{ @Override public void sell() { System.out.println("Customer sell"); } @Override public void buy() { System.out.println("Customer Buy"); } @Override public void order() { System.out.println("Customer Order"); } public void sayHello(){ System.out.println("Say Hello"); } } public class CustomerTest { public static void main(String[] args) { Customer customer = new Customer(); customer.sell(); customer.buy(); customer.sayHello(); Buy buyer = customer; Sell seller = customer; seller.sell(); customer.order(); } }
public interface Sell {
    void sell();
    default void order(){
        System.out.println("Seller Order");
    }
}

public interface Buy {
    void buy();
    default void order(){
        System.out.println("Buyer Order");
    }
}

public class Customer implements Buy,Sell{

    @Override
    public void sell() {
        System.out.println("Customer sell");
    }
    @Override
    public void buy() {
        System.out.println("Customer Buy");
    }
    @Override
    public void order() {
        System.out.println("Customer Order");
    }
    public void sayHello(){
        System.out.println("Say Hello");
    }
}

public class CustomerTest {
    public static void main(String[] args) {
        Customer customer = new Customer();
        customer.sell();
        customer.buy();
        customer.sayHello();
        Buy buyer = customer;
        Sell seller = customer;
        seller.sell();

        customer.order();
    }
}

– 클래스 상속과 인터페이스 구현 함께 쓰기

  • 실무에서 프레임워크나 오픈소스와 함께 연동되는 구현을 하게 되면 클래스 상속과 인터페이스의 구현을 같이 사용하는 경우가 많음
  • 책이 순서대로 대여가 되는 도서관 구현
  • 책을 보관하는 자료 구조가 shelf에 구현됨(ArrayList)
  • Queue 인터페이스를 구현함
  • shelf 클래스를 상속받고 Queue 를구현한다
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public interface Queue {
void enQueue(String title);
String dnQueue();
int getSize();
}
import java.util.ArrayList;
public class Shelf {
protected ArrayList<String> shelf;
public Shelf(){
shelf = new ArrayList<String>();
}
public ArrayList<String> getShelf(){
return shelf;
}
public int getCount(){
return shelf.size();
}
}
public class BookSelf extends Shelf implements Queue {
@Override
public void enQueue(String title) {
shelf.add(title);
}
@Override
public String dnQueue() {
return shelf.remove(0);
}
@Override
public int getSize() {
return 0;
}
}
public class BookShelfTest {
public static void main(String[] args) {
Queue bookQueue = new BookSelf();
bookQueue.enQueue("태백산맥1");
bookQueue.enQueue("태백산맥2");
bookQueue.enQueue("태백산맥3");
System.out.println(bookQueue.dnQueue());
System.out.println(bookQueue.dnQueue());
System.out.println(bookQueue.dnQueue());
}
}
public interface Queue { void enQueue(String title); String dnQueue(); int getSize(); } import java.util.ArrayList; public class Shelf { protected ArrayList<String> shelf; public Shelf(){ shelf = new ArrayList<String>(); } public ArrayList<String> getShelf(){ return shelf; } public int getCount(){ return shelf.size(); } } public class BookSelf extends Shelf implements Queue { @Override public void enQueue(String title) { shelf.add(title); } @Override public String dnQueue() { return shelf.remove(0); } @Override public int getSize() { return 0; } } public class BookShelfTest { public static void main(String[] args) { Queue bookQueue = new BookSelf(); bookQueue.enQueue("태백산맥1"); bookQueue.enQueue("태백산맥2"); bookQueue.enQueue("태백산맥3"); System.out.println(bookQueue.dnQueue()); System.out.println(bookQueue.dnQueue()); System.out.println(bookQueue.dnQueue()); } }
public interface Queue {
    void enQueue(String title);
    String dnQueue();
    int getSize();
}

import java.util.ArrayList;

public class Shelf {
    protected ArrayList<String> shelf;

    public Shelf(){
        shelf = new ArrayList<String>();
    }
    public ArrayList<String> getShelf(){
        return shelf;
    }
    public int getCount(){
        return shelf.size();
    }
}

public class BookSelf extends Shelf implements Queue {
    @Override
    public void enQueue(String title) {
        shelf.add(title);
    }

    @Override
    public String dnQueue() {
        return shelf.remove(0);
    }

    @Override
    public int getSize() {
        return 0;
    }
}

public class BookShelfTest {
    public static void main(String[] args) {
        Queue bookQueue = new BookSelf();
        bookQueue.enQueue("태백산맥1");
        bookQueue.enQueue("태백산맥2");
        bookQueue.enQueue("태백산맥3");

        System.out.println(bookQueue.dnQueue());
        System.out.println(bookQueue.dnQueue());
        System.out.println(bookQueue.dnQueue());
    }
}

댓글 남기기