day19_DefaultMethodTest18

육희영·2021년 10월 28일
0
package com.java1.day19;

//default 메서드..
//접근 제어자가 public 이며 생략 가능하다.
public class DefaultMethodTest18 {
	public static void main(String[] args) {
		
		Child c = new Child();
		c.method1();
		c.method2();
		MyInterface1.staticMethod();
		MyInterface2.staticMethod();
	}
}

class Child extends Parent implements MyInterface1, MyInterface2 {
	//인터 페이스를 구현한 클래스 에서 디폴트 메서드를 오버라이딩 해야 한다.
	@Override
	public void method1() {		
		System.out.println("method1() in Child");
	}
	
	//Parent 클래스의 method2 메서드와 MyInterface 인터페이스의 method2 메서드가 중복 이다.
	//이 경우 조상 클래스의 메서드가 상속되고, 디폴드 메서드는 무시된다.
	@Override
	public void method2() {
		System.out.println("method2() in Child");
	}
}

class Parent {
	public void method2() {
		System.out.println("method2() in Parent");
	}
}

interface MyInterface1 {
	default void method1() {
		System.out.println("method1() in MyInterface1");
	}
	default void method2() {
		System.out.println("method2() in MyInterface1");
	}
	static void staticMethod() {
		System.out.println("staticMethod() in MyInterface1");
	}
}

interface MyInterface2 {
	default void method1() {
		System.out.println("method1() in MyInterface2");
	}
	static void staticMethod() {
		System.out.println("staticMethod() in MyInterface2");
	}
}

출력결과

method1() in Child
method2() in Child
staticMethod() in MyInterface1
staticMethod() in MyInterface2

0개의 댓글

관련 채용 정보