ocjap를 위한 자바 기초/oca

ocajp default 인터페이스와 override, overload

xemaker 2020. 9. 8. 18:09
interface Aquatic{
	public default int getNumberOfGills(int input) { return 2; }
}
public class ClownFish implements Aquatic {
	public String getNumberOfGills() { return "4"; }
	public String getNumberOfGills(int input) { return "6"; }
	public static void main(String[] args) {
		System.out.println(new ClownFish().getNumberOfGills(-1));
	}
}

What is the output of the following code? (Choose all that apply)

A) The code will not compile because of line 6.
B) The code will not compile because of line 5.
C) 4
D) The code will not compile because of line 8.
E) 2
F) 6

 

해설:

이 코드는 6번째 줄이 Aquatic 인터페이스에 정의된 getNumberOfGills(int input) 메소드와 호환되지 않는 오버라이드이기 때문에 컴파일이 되지 않는다.  자세히 말하면, int는 String의 서브 클래스가 아니기 때문에 int와 String은 동일하지 않는 리턴 타입이다. 5째 라인은 이슈없이 컴파일 된다. 이유는 getNumberOfGills()는 int를 취하는 부모 인터페이스 메소드와 관계 없는 오버로드된 메소드 이기 때문이다. 

 

정답: A