# super - 상속과 생성자 super(name,hp) 대신에 this.name = name; this.hp = hp; 를 대체했더니 오류가 발생하였습니다. 왜 불가능 한지 궁금 합니다. #### CODE <a class='btn btn-default' href='/codes/58546'>Link</a> ``` public class SuperTest { public static void main(String[] args) { /* 1. Orc 객체를 만들고 정보를 출력하시오. */ Orc orc = new Orc("오크",80) ; System.out.println(orc.toString()) ; /* 2. OrcWarrior 객체를 만들고 정보를 출력하시오. */ OrcWarrior ow = new OrcWarrior("오크전사",120,3) ; System.out.println(ow.toString()) ; } } class Orc { protected String name; protected int hp; public Orc(String name, int hp) { this.name = name; this.hp = hp; } public String toString() { return String.format("Orc { name: %s, hp: %d }", this.name, this.hp); } } class OrcWarrior extends Orc { protected int amor; public OrcWarrior(String name, int hp, int amor) { super(name,hp); this.amor = amor; } // 메소드 오버라이딩! public String toString() { return String.format("OrcWarrior { name: %s, hp: %d, amor: %d }", super.name, super.hp, this.amor); } } ``` #### INPUT ``` ``` #### OUPUT ``` Orc { name: 오크, hp: 80 } OrcWarrior { name: 오크전사, hp: 120, amor: 3 } ```
### 상속이 적용된 객체는 생성자의 첫줄에서 반드시 부모의 생성자를 호출해야 합니다. (또는 또 다른 생성자를 호출) 자세한 내용은 "자바 상속과 super 생성자" 정도로 구글링해보세요.