오버라이딩이란, 상속받은 메서드의 내용을 변경하는 것을 말한다.
한마디로, 선언부가 일치해야 한다.
주의할 점으로는,
super는 자손 클래스에서 조상 클래스로부터 상속받은 멤버를 참조하는데 사용되는 참조변수이다. 상속받은 멤버와 자신의 멤버의 이름이 같을 때 super를 붙여서 구별하는 용도로 쓰인다.
super()는 조상 클래스의 생성자를 호출하는 데 사용된다.
모든 클래스의 생성자 첫 줄에 생성자, this() 또는 super()를 호출해야 한다.
class Parent { // extends Object (모든 생성자는 Object 클래스를 상속함)
protected String name;
protected String health;
public Parent(String name, String health) {
// super(); 자동 생성됨 (Object()를 호출하는 것)
this.name = name;
this.health = health;
}
}
class Child extends Parent {
protected String gender;
public Child(String name, String health, String gender){
super(name, health); // Child가 초기화되려면 Parent부터 초기화시켜주어야 함
this.gender = gender;
}
}