nme.kr

문서의 이전 판입니다!


Chaptor08 상속

8.1 클래스 상속

:wr:Phone.java : 부모 클래스

package chapter08;

public class Phone {

	String name;
	String color;
	String company;
	
	void call() {
		System.out.println("전화를 건다");
	}
	
	void receive() {
		System.out.println("전화를 받다");
	}
	
}

:wr:SmartPhone.java : 자식 클래스

package chapter08;

public class SmartPhone extends Phone {

	public void installApp() {
		System.out.println("앱 설치");
	}
	
}

:wr:SmartPhoneMain.java

package chapter08;

public class SmartPhoneMain {

	public static void main(String[] args) {
		
		Phone p = new Phone();
		p.name = "전화기";
		p.company = "현대";
		p.color = "화이트";
		
		System.out.println("Phone 출력");
		System.out.println(p.name);
		System.out.println(p.company);
		System.out.println(p.color);
		p.call();
		p.receive();
		
		SmartPhone sp = new SmartPhone();
		sp.name = "갤럭시";
		sp.company = "삼성";
		sp.color = "블랙";
		
		System.out.println("SmartPhone 출력");
		System.out.println(sp.name);
		System.out.println(sp.company);
		System.out.println(sp.color);
		sp.call();
		sp.receive();
		sp.installApp();
		
	}
}

package chapter08;

public class SuperEx {

	public static void main(String[] args) {
		
		Child child = new Child();
		child.print();
		
	}

}

class Parent {
	
	int number = 3;
	
	Parent() {
		System.out.println("부모 객체 생성");
	}
	
}

class Child extends Parent {
	
	//int number = 2;
	
	Child() {
		System.out.println("자식 객체 생성");
	}
	
	void print() {
		int number = 1;
		System.out.println(number); // 메서드 지역변수 number
		System.out.println(this.number); // 자신 객체의 number
		System.out.println(super.number); // 부모 객체의 number
		
	}
}

package chapter08;

public class Child extends Parent {
	
	// int number = 2;
	
	Child(){
		System.out.println("자식 객체 생성");
	}
	
	viod print() {
		int number = 1;
		System.out.println(number); // 메서드 지역변수 number
		System.out.println(this.number); // 자신 객체의 number
		System.out.println(super.number); // 부모 객체의 number
	}

}

super()

package chapter08;

public class SuperEx2 {

}

class Parent2 {
	
	String name;
	
	Parent2(String name) {
		this.name = name;
	}
	
}

class Child2 extends Parent2 {
	
	Child2(String name) {
		super(name);
	}
	
}

8.2 메서드 재정의




8.3 다형성










8.4 상속관계에서 접근제한자





8.5 추상클래스


8.6 객체를 배열로 처리




8.2 메서드 재정의


8.3 다형성


8.4 상속관계에서 접근제한자


8.5 추상클래스


8.6 객체를 배열로 처리


8.7 final 제어자