- 상위 변수는 직계 상위 클래스 인스턴스를 나타냅니다.
- 상위 변수는 바로 상위 클래스 메서드를 호출할 수 있습니다.
- super()는 직계 상위 클래스 생성자 역할을 하며 하위 클래스 생성자의 첫 번째 라인에 있어야 합니다.
재정의된 메서드의 슈퍼클래스 버전을 호출할 때 super 키워드가 사용됩니다.
예시
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
super.move(); // invokes the super class method
System.out.println("Dogs can walk and run");
}
}
public class TestDog {
public static void main(String args[]) {
Animal b = new Dog(); // Animal reference but Dog object
b.move(); // runs the method in Dog class
}
} 출력
이렇게 하면 다음과 같은 결과가 생성됩니다. -
Animals can move Dogs can walk and run