Interface와 조상 클래스의 충돌

노을·2021년 2월 16일
0

JAVA

목록 보기
2/3

1. Interface와 조상클래스의 멤버변수 충돌하는 경우

  • 인터페이스는 static 상수만 정의할 수 있으므로, 충돌이 날 경우 클래스 이름을 붙여서 구분 가능
  • 자바의 정석 3판, 385p

2. Interface와 조상클래스의 메소드가 충돌하는 경우

  • 인터페이스의 추상메서드는 구현내용이 전혀 없으므로 조상클래스 쪽의 메서드를 상속받으면 됨.
  • 자바의 정석 3판, 385p

이해하기 위해 코드로 짜보았다.

  • 클래스, 멤버변수, 메서드 이름은 의미가 없고, 충돌에 목적을 두고 설계함.
  • 사실 정확한지 모르겠습니다, 틀린게 있다면 피드백 부탁드립니다!
class Human{
    int age = 15;
    void eat(){
        System.out.println("Human");
    }
}

interface HumanAble {
    public static final int age = 20;
    public abstract void eat();
}

class sanhee extends Human implements HumanAble{
    @Override
    public void eat() {
        super.eat();
    }

    //m Reference to 'age' is ambiguous, both 'Human.age' and 'HumanAble.age' match
    //int age_ = age;

    int age_0 = HumanAble.age;
    int age_1 = super.age;
}

public class Practice {

    public static void main(String[] args) {
        sanhee sanhee = new sanhee();

        System.out.println("HumanAble.age:" +sanhee.age_0);
        System.out.println("super.age:" +sanhee.age_1);
    }
}
profile
카르페디엠

0개의 댓글