상속
- 기본의 클래스에 기능을 추가하거나 재정의하여 새로운 클래스를 정의하는 것
상속의 장점
- 클래스 재사용
- 자식 클래스 설계 시 중복되는 멤버를 미리 부모 클래스에 작성해 놓으면 자식 클래스에선 해당 멤버를 작성하지 않아도 된다.
- 클래스 간의 계층적 관계를 구성함으로써 다형성의 문법적 토대를 마련한다.
자식 클래스
- 부모 클래스의 모든 특성을 물려받아 새롭게 작성된 클래스
class 자식클래스이름 extends 부모클래스이름{
...
}
class myPhone extends Phone{
...
}
- 부모 클래스, 자식 클래스의 관계

--> 필드오 메서드만 상속되며 생성자와 초기화 블록은 상속되지 않는다.
super
- 부모 클래스로부터 상속받은 필드나 메서드를 자식 클래스에서 참조하는 데 사용하는 참조 변수
class Parent {
int a = 10;
}
class Child extends Parent {
int a = 20;
void display() {
System.out.println(a);
System.out.println(this.a);
System.out.println(super.a);
}
}
public class Inheritance03 {
public static void main(String[] args) {
Child ch = new Child();
ch.display();
}
}
// 결과
20
20
10
- this참조 변수는 자식 클래스에서 대입된 값을 출력하고 super 참조 변수만이 부모 클래스에서 대입된 값을 출력하게 된다.
super() 메서드
class Parent {
int a;
Parent() {
a = 10;
}
Parent(int n) {
a = n;
}
}
class Child extends Parent {
int b;
Child() {
① //super(40);
b = 20;
}
void display() {
System.out.println(a);
System.out.println(b);
}
}
public class Inheritance04 {
public static void main(String[] args) {
Child ch = new Child();
ch.display();
}
}
// 결과
10
20