Part12 - Inheritance


Ex01_inheritance

package com.mywork.ex;

// 부모클래스 (슈퍼클래스)
class Parent {
	
	// Field
	int number;
	
	// Method
	void doParent() {
		System.out.println("doParent() 호출");
	}
}

// 자식클래스 (서브클래스)
class Child extends Parent {
	
	// Method
	void doChild() {
		System.out.println("doChild() 호출");
	}
}


public class Ex01_inheritance {

	public static void main(String[] args) {
		// 자식클래스의 객체(인스턴스) 생성 및 사용
		Child child = new Child();
		
		child.number = 10;
		System.out.println(child.number);
		
		child.doParent();
		child.doChild();
	}

}

Ex02_inheritance

package com.mywork.ex;

class Person{
	
	// Method
	void sleep(){
		System.out.println("잔다.");
	}
	void eat(String food) {
		System.out.println(food + " 먹는다.");
	}
}

class Student extends Person {
	
	// Method
	void study() {
		System.out.println("공부한다.");
	}
	
}

public class Ex02_inheritance {

	public static void main(String[] args) {
		
		Student student = new Student();
		
		student.sleep();
		student.eat("빵");
		student.study();
		
	}

}

Ex03_inheritance

package com.mywork.ex;
class Car{
	// Method
	
	void drive() {
		System.out.println("차가 달린다.");
	}
}
class Ev extends Car{
	// Method
	void charging() {
		System.out.println("전기를 충전한다.");
	}
	
}
public class Ex03_inheritance {

	public static void main(String[] args) {
		Ev ev = new Ev();
		
		ev.drive();
		ev.charging(); 
		
	}

}

Ex04_constructor

package com.mywork.ex;

/*
 * 자식클래스의 생성자는 부모클래스의 생성자를 먼저 호출한다.
 * (자식이 만들어지려면 부모 먼저 만들어야 한다.)
 */

class Mother{
	// Constructor
	Mother(){
		System.out.println("Mother 생성!");
	}
}

class Son extends Mother{
	
	// Constructor
	Son(){
		System.out.println("Son 생성!");
	}
	
	// Method
	void doSon() {
		System.out.println("doSun() 호출!");
	}
	
}
public class Ex04_constructor {

	public static void main(String[] args) {
		Son son = new Son();
		son.doSon();
		
		
	}

}

Ex05_contructor

package com.mywork.ex;

/*
 * super
 * 
 *  1. 자식클래스가 알고 있는 부모클래스의 참조(주소)
 *  2. 사용
 *      1) super.필드     : 부모클래스의 필드 사용
 *      2) super.메소드()  : 부모클래스의 메소드 사용
 *      3) super()       : 부모클래스의 생성자 사용
 */
class Animal{
	
	// Field
	String name;
	
	// Constructor
	Animal() {}            // 기본 생성자 생성!
	Animal(String name){
		this.name = name;
	}
}

class Dog extends Animal{
	
	// Field
	String personName;
	
	// Constructor
	Dog(String personName){
		super();        // 슈퍼클래스의 생성자 : Animal(){ } 호출!
		this.personName = personName;
	}
	Dog(String name, String personName){
		super(name);    // 슈퍼클래스의 생성자 : Animal(String name){ } 호출!
		this.personName = personName;
	}
	
	// Method
	void whoAmI() {
		System.out.println("내 이름은 " + name + "이고, 주인은 " + personName + "입니다.");
	}
}

public class Ex05_contructor {

	public static void main(String[] args) {
		
		Dog dog = new Dog("alice");
		dog.whoAmI();
		
		Dog dog2 = new Dog("이쁜이", "이말자");
		dog2.whoAmI();
		
	}

}

Ex06_has_a

package com.mywork.ex;

class Engine{
	
	// Field
	String fuel;
	
	// Constructor
	Engine(String fuel){
		this.fuel = fuel;
	}
	
	// Method
	void engineInfo() {
		System.out.println("사용 연료 : " + fuel);
		System.out.println(fuel.equals("가솔린") ? "가솔린엔진" : "디젤엔진" );
	}
	
}

class Suv extends Engine{
	
	// Field
	String model;
	
	// Constructor
	Suv(String fuel, String model){
		super(fuel);
		this.model = model;
	}
	
	// Method
	void suvInfo() {
		System.out.println("모델 : "+ model);
		super.engineInfo();
		
	}
}

public class Ex06_has_a {
	public static void main(String[] args) {
		Suv suv1 = new Suv("가솔린", "싼타페");
		Suv suv2 = new Suv("디젤", "쏘렌토");
		
		suv1.suvInfo();
		suv2.suvInfo();
		
	}

}

Ex07_method_override

package com.mywork.ex;

class Espresso {
	
	// Field
	String origin;
	
	// Constructor
	Espresso(String origin) {
		this.origin = origin;
	}
	
	// Method
	void taste() {
		System.out.println("강렬하다");
	}
}

class Latte extends Espresso {
	// Field
	String milk;
	
	// Constructor
	Latte(String origin, String milk){
		super(origin);
		this.milk = milk;
	}
	
	// Method
//	@Override -> 실수 방지를 위해서 나 혹은 다른 개발자들에게 알리는 용도.
	void taste() {
		System.out.println("부드럽다");
	}
}

public class Ex07_method_override {

	public static void main(String[] args) {
		Latte latte = new Latte("인도네시아", "서울우유");
		latte.taste();
	}

}