목차

Ch.08 상속

클래스 상속

: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);
	}
	
}

메서드 재정의

Car.java

package chapter08;

public class Car {

	String color;
	String name;
	
	public void go() {
		System.out.println("전진");
	}
	
	void back() {
		System.out.println("후진");
	}
}

Taxi.java

package chapter08;

public class Taxi extends Car {
	
	public void go() {
		System.out.println("미터기를 켜고 전진");
	}
}

TaxiMain.java

package chapter08;

public class TaxiMain {
	
	public static void main(String[] args) {
		
		Taxi t = new Taxi();
		t.go();
		
		int a = 10;
		double b = a; // 자동형변환

		double c = 10.5;
//		int d = c; // 에러

		//강제형변환
		int d = (int)c;
		
		System.out.println(b);
		System.out.println(d);
		
	}
}

다형성

Parent.java

package chapter08.poly;

public class Parent {

	String name;
	
	void walk() {
		System.out.println("부모가 걷는다.");
	}
	
	void run() {
		System.out.println("부모가 달린다.");
	}
	
}

Child.java

package chapter08.poly;

public class Child extends Parent {

	String name;
	
	// 재정의 메서드
	void run() {
		System.out.println("자식이 달린다.");
	}
	
	// 추가된 메서드
	void eat() {
		System.out.println("자식이 먹는다.");
	}
}

PolyEx.java

package chapter08.poly;

public class PolyEx {

	public static void main(String[] args) {
		
		Child c = new Child();
		
		c.run();
		
		// 부모클래스의 자료형으로 선언 (자동형변환)
		Parent p = new Child();
		p.run(); // 재정이된 메서드가 실행
		// p.eat(); // 에러
		

	}

}

package chapter08.poly;

public class PolyEx2 {

	public static void main(String[] args) {
		
		Parent p = new Child();
		
		p.run();
		
		// 자식클래스의 자료형으로 변환 (강제형변환)
		Child c = (Child)p;
		c.eat();

	}

}

package chapter08.poly;

public class GraphicCard {

	int memory;
	
	public void process() {
		System.out.println("그래픽 처리");
	}
	
}
                                                                                                                                                                                                                                                           

package chapter08.poly;

public class Amd extends GraphicCard {

	public void process() {
		System.out.println("AMD 그래픽 처리");
	}
	
}

package chapter08.poly;

public class Nvidia extends GraphicCard {

	public void process() {
		System.out.println("Nvidia 그래픽 처리");
	}
	
}

:wr:매개변수의 다형성

package chapter08.poly;

public class Game {

	void display(GraphicCard gc) {
		gc.process();
	}
	
}

package chapter08.poly;

public class Computer2 {

	public static void main(String[] args) {
		
		Game g = new Game();
		
		GraphicCard gc = new GraphicCard();
		g.display(gc);
		
		Amd gc2 = new Amd();
		g.display(gc2);
		
		Nvidia gc3 = new Nvidia();
		g.display(gc3);

	}

}

package chapter08.poly;

public class ObjectEx {

	public static void main(String[] args) {
		
		allObject(new GraphicCard());
		allObject(new Amd());
		allObject(new Nvidia());
		allObject("안녕");

	}
	
	public static void allObject(Object obj) {
		
		System.out.println(obj.toString());
		
	}

}

상속관계에서 접근제한자

Aclass

package chapter08.pkg1;

public class Aclass {

	protected String varA;
	String varA2;
	
	protected void methodA() {
		System.out.println("methodA");
	}
	
	void methodA2() {
		System.out.println("methodA2");
	}
}

AclassMain

package chapter08.pkg1;

public class AclassMain {

	public static void main(String[] args) {
		
		Aclass ac = new Aclass();
		ac.varA = "varA";
		ac.varA2 = "varA2";
		ac.methodA();
		ac.methodA2();
		
	}
}

Bclass

package chapter08.pkg2;

import chapter08.pkg1.Aclass;

public class Bclass {
	
	public void methodB() {
		Aclass ac = new Aclass();
		//ac.varA = "varA"; // 사용 불가
		//ac.varA2 = "varA2"; // 사용 불가
		//ac.methodA(); // 사용 불가
		//ac.methodA2(); // 사용 불가
	}
	
}

CClass

package chapter08.pkg2;

import chapter08.pkg1.Aclass;

public class CClass extends Aclass {

	CClass() {
		this.varA = "varA"; // 사용 가능
		//this.varA2 = "varA2"; // 사용 불가
		this.methodA(); // 사용 가능
		//this.methodA2(); // 사용 불가
	}
}

추상클래스

접근제한자 abstract 리턴타입 메서드명(매개변수);

package chapter08;

abstract class Shape {
	String type;
	Shape(String type) {
		this.type = type;
	}
	abstract double area();
	abstract double length();
}
class Circle extends Shape{
	int r;
	Circle(int r) {
		super("원");
		this.r = r;
	}
	@Override
	double area() {
		return r * r * Math.PI;
	}
	@Override
	double length() {
		return 2 * r * Math.PI;
	}
	@Override
	public String toString() {
		return "Shape [type=" + type + ", r=" + r + "]";
	}
}

class Rectangle extends Shape {
	int width, height;
	Rectangle(int width, int height) {
		super("사각형");
		this.width = width;
		this.height = height;
	}

	@Override
	double area() {
		return width * height;
	}
	@Override
	double length() {
		return 2 * (width + height);
	}
	@Override
	public String toString() {
		return "Shape [type=" + type + ", width=" + width + ", height=" + height+"]";
	}

}

public class ShapeEx {
	public static void main(String[] args) {
		Shape[] shapes = new Shape[2];
		shapes[0] = new Circle(10);
		shapes[1] = new Rectangle(5,5);
		for(Shape s : shapes) {
			System.out.println(s);
			System.out.println("넓이:"+s.area()+" 둘레:"+s.length());
		}
	}
}

객체를 배열로 처리

Animal.java

package chapter08;

public class Animal {

	String type;
	String name;
	
	Animal(String type, String name) {
		this.type = type;
		this.name = name;
	}
	
	void sleep() {
		System.out.println(this.name +"은(는) 잠을 잔다.");
	}
}

Eagle.java

package chapter08;

public class Eagle extends Animal {

	Eagle(String type, String name) {
		super(type, name);
	}
	
	void sleep() {
		System.out.println(this.name +"은(는) 하늘에서 잠을 잔다.");
	}

	
}

Tiger.java

package chapter08;

public class Tiger extends Animal {

	Tiger(String type, String name) {
		super(type, name);
	}
	
	void sleep() {
		System.out.println(this.name +"은(는) 산속에서 잠을 잔다.");
	}

	
}

Lion.java

package chapter08;

public class Lion extends Animal {

	Lion(String type, String name) {
		super(type, name);
	}
	
	void sleep() {
		System.out.println(this.name +"은(는) 숲속에서 잠을 잔다.");
	}

	
}

Shark.java

package chapter08;

public class Shark extends Animal {

	Shark(String type, String name) {
		super(type, name);
	}
	
	void sleep() {
		System.out.println(this.name +"은(는) 물속에서 잠을 잔다.");
	}

	
}

AnimalMain.java

package chapter08;

public class AnimalMain {

	public static void main(String[] args) {
		
		Animal[] ani = new Animal[4];
		
		Animal eagle = new Eagle("조류", "독수리");
		Animal tiger = new Tiger("포유류", "호랑이");
		Animal lion = new Lion("포유류", "사자");
		Animal shark = new Shark("어류", "상어");
		
		ani[0] = eagle;
		ani[1] = tiger;
		ani[2] = lion;
		ani[3] = shark;
		
		for (int i=0; i<ani.length; i++) {
			ani[i].sleep();
		}
	}
}

final 제어자

public class SubClass extends String { // 사용불가

package chapter08;

public class FinalMethod {

	// 재정의 가능한 메서드
	void method() {
		
	}
	
	// 재정의가 불가능한 메서드
	final void finalMethod() {
		
	}
}

class SubFinalMethod extends FinalMethod {
	
	void method() { // 재정의 가능
		System.out.println("method() 재정의");
	}
	
	/*
	void finalMethod() { // 재정의 불가
		System.out.println("finalMethod() 재정의");
	}
	*/
}