17. 자바(JAVA) - 인터페이스의 여러가지 요소

병호·2023년 7월 14일

자바

목록 보기
17/24

상수

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

double PI = 3.14;
int ERROR = -999999999;

추상 메서드

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

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

1. 구현을 가지는 메서드, 인터페이스를 구현하는 클래스들에서 공통으로 사용할 수 있는 기본 메서드

2. default 키워드 사용

default void description() {
	System.out.println("정수 계산기를 구현합니다.");
	myMethod();
}

3. 구현 하는 클래스에서 재정의 할 수 있음

@Override
public void description() {
	System.out.println("CompleteCalc에서 재정의한 default 메서드");
	//super.description();
}

4. 인터페이스를 구현한 클래스의 인스턴스가 생성 되어야 사용 가능함

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

인스턴스 생성과 상관 없이 인터페이스 타입으로 사용할 수 있는 메서드

static int total(int[] arr) {
	int total = 0;
		
	for(int i: arr) {
		total += i;
	}
	mystaticMethod();
	return total;
}

private 메서드 (자바 9이후)

1. 인터페이스를 구현한 클래스에서 사용하거나 재정의 할 수 없음

2. 인터페이스 내부에서만 사용하기 위해 구현하는 메서드

3. default 메서드나 static 메서드에서 사용함

private void myMethod() {
	System.out.println("private method");
}
	
private static void mystaticMethod() {
	System.out.println("private static method");
}

여러 인터페이스 구현하기, 인터페이스의 상속

1. 여러 인터페이스 구현

1. 자바의 인터페이스는 구현 코드가 없으므로 하나의 클래스가 여러 인터페이스는 구현 할 수 있음

2. 디폴트 메서드가 중복 되는 경우는 구현 하는 클래스에서 재정의 하여야 함

3. 여러 인터페이스를 구현한 클래스는 인터페이스 타입으로 형 변환 되는 경우 해당 인터페이스에 선언된 메서드만 사용 가능 함

Sell.java

public interface Sell {

	void sell();
	
	
}

Buy.java

public interface Buy {

	void buy();

}

Customer.java

public class Customer implements Buy, Sell{

	@Override
	public void sell() {
		System.out.println("customer sell");
	}

	@Override
	public void buy() {
		System.out.println("customer buy");		
	}

	public void sayHello() {
		System.out.println("Hello");
	}
}

CustomerTest.java

public class CustomerTest {

	public static void main(String[] args) {

		Customer customer = new Customer();
		customer.buy();
		customer.sell();
		customer.sayHello();
		
		Buy buyer = customer;
		buyer.buy();
		
		Sell seller = customer;
		seller.sell();

	}
}

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

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

Sell.java

public interface Sell {

	void sell();
	
	default void order() {
		System.out.println("판매 주문");
	}
}

Buy.java

public interface Buy {

	void buy();

	default void order() {
		System.out.println("구매 주문");
	}
}

Customer.java

public class Customer implements Buy, Sell{

	@Override
	public void sell() {
		System.out.println("customer sell");
	}

	@Override
	public void buy() {
		System.out.println("customer buy");		
	}

	public void sayHello() {
		System.out.println("Hello");
	}

	@Override
	public void order() {
		System.out.println("customer order");
	}

}

CustomerTest.java

public class CustomerTest {

	public static void main(String[] args) {

		Customer customer = new Customer();
		customer.buy();
		customer.sell();
		customer.sayHello();
		
		Buy buyer = customer;
		buyer.buy();
		
		Sell seller = customer;
		seller.sell();
	
		buyer.order();
		seller.order();
	
	}
}

인터페이스의 상속

인터페이스 사이에도 상속을 사용할 수 있음

extends 키워드를 사용

인터페이스는 다중 상속이 가능하고 구현 코드의 상속이 아니므로 타입 상속 이라고 함

X.java

public interface X {

	void x();
}

Y.java

public interface Y {

	void y();
}

MyInterface.java

public interface MyInterface extends X, Y{

	void myMethod();
}

MyClass.java

public class MyClass implements MyInterface{

	@Override
	public void x() {
		System.out.println("x()");
	}

	@Override
	public void y() {
		System.out.println("y()");		
	}

	@Override
	public void myMethod() {
		System.out.println("myMethod()");		
	}
}

MyClassTest.java

public class MyClassTest {

	public static void main(String[] args) {

		MyClass mClass = new MyClass();
		
		X xClass = mClass;
		xClass.x();
		
		
		Y yClass = mClass;
		yClass.y();
		
		MyClass iClass = mClass;
		iClass.x();
		iClass.y();
		iClass.myMethod();
	}

}
profile
안녕하세요

0개의 댓글