상속 (inheritance)

: 부모 클래스에 만들어진 필드, 메소드를 자식 클래스가 물려받음

상속의 장점

상속의 특징

상속 선언

public class Person {
	...
}
public class Student extends Person { // Person 을 상속받는 클래스 Student 선언
	...
}
public class StudentWorker extends Student { // Student 를 상속받는 StudentWorker 선언
	...
}

Ex 1

public class ColorPointEx {
    public static void main(String[] args) {
        Point p = new Point();
        p.set(1,2);
        p.showPoint();

        ColorPoint cp = new ColorPoint();
        cp.set(3,4);
        cp.setColor("red");
        cp.showColorPoint();
    }
}
class Point{
    private int x, y;
    public void set(int x, int y){
        this.x = x;
        this.y = y;
    }
    public void showPoint(){
        System.out.println("("+x+","+y+")");
    }
}
class ColorPoint extends Point {
    private String color;
    public void setColor(String color){
        this.color = color;
    }
    public void showColorPoint(){
        System.out.print(color);
        showPoint();
    }
}