super(param)

leverest96·2022년 11월 30일
0

Spring / Java

목록 보기
6/20
post-thumbnail

부모 생성자가 기본 생성자가 없고 매개 변수가 있는 생성자만 있다면 자식 생성자에서 반드시 필요하다.

People.java (부모클래스)

public class People {
    public String name;
    public int age;

    public People(String name, int age) {  // 기본 생성자가 없고 두개의 매개 변수를 받는 생성자가 있다.
        this.name = name;
        this.age = age;
    }
}

Student.java (자식 클래스)

public class Student extends People {
    public int score;

    public Student(String name, int age, int score) {  // 3개의 매개변수를 받는 생성자
        super(name, age);  // name과 age는 부모 생성자를 호출하는 super()를 이용한다.
        this.score = score;
    }
}

https://cl0clam.tistory.com/36

profile
응애 난 애기 개발자

0개의 댓글