상속(Inheritance)

기존의 클래스로 새로운 클래스를 작성하여 코드를 재사용하는 것

두 클래스간에 부모-자식 관계를 맺어주는 것

포함(Composite)

클래스의 멤버로 참조변수를 선언하는 것

class Tv {
	boolean power;
	int channel;
	void power() 		{ power = !power; }
	void channelUp() 	{ ++channel; }
	void channelDown() 	{ --channel; }
}

class SmartTv { // Tv를 포함하고 있음
	Tv tv = new Tv();
	boolean caption;
	void displayCaption(String text) {
		if (caption) {
			System.out.println(text);
		}
	}
}

public class ch7_1 {
	public static void main(String[] args) {
		SmartTv stv = new SmartTv();
		stv.tv.power();
		stv.tv.channel = 9;
		stv.tv.channelUp();
		stv.caption = !stv.caption;
		stv.displayCaption("hello world");
		System.out.println(stv.tv.power + ", " + stv.tv.channel);
	}
}

상속 관계 vs 포함 관계

[자바의 정석 - 기초편] ch7-3,4 클래스 간의 관계, 상속과 포함

단일 상속(Single Inheritance)