반응형
- super
- super(매개값...)은 매개값과 동일한 타입, 개수, 순서가 맞는 부모 생성자를 호출
- 만약 부모 생성자가 없다면 컴파일 오류가 발생
- 반드시 자식 생성자의 첫줄에 위치해야 한다.
- 부모 클래스에 기본(매개변수X) 생성자가 없다면 필수적으로 작성해야 함.
- 실습
1. 부모 클래스 생성
public class People {
public String name;
public String ssn;
public People(String name, String ssn) {
this.name = name;
this.ssn = ssn;
System.out.println("부모 객체 생성 완료");
}
}
2. 자식 클래스 생성
부모 클래스를 상속 해준다.
public class Student extends People {
public int studentNo;
public Student(String name, String ssn, int studentNo) {
super(name, ssn);
this.studentNo = studentNo;
System.out.println("자식 객체 생성 완료");
}
}
3. 실행 클래스
public class StudentExample {
public static void main(String[] args) {
Student student = new Student("홍길동", "123456-1234567", 1);
System.out.println("name: " + student.name);
}
}
실행해보면
반응형
'study > JAVA' 카테고리의 다른 글
상속(Inheritance) - 클래스 상속 (0) | 2022.02.24 |
---|---|
어노테이션(Annotation) (0) | 2022.02.22 |
Getter와 Setter (0) | 2022.02.18 |
접근 제한자( Access Modifier) (0) | 2022.02.18 |
상수(static final) (0) | 2022.02.14 |
댓글