업캐스팅

package ex06.MyExample;

class Shape {
    protected int x, y;

    public void draw() {
        System.out.println("Shape Draw");
    }
}

class Rectangle extends Shape {
    private int base, height;

    public void draw() {
        System.out.println("Rectangle Draw");
    }
}

class Triangle extends Shape {
    private int base, height;

    public void draw() {
        System.out.println("Triangle Draw");
    }
}

class Circle extends Shape {
    private int radius;

    public void draw() {
        System.out.println("Circle Draw");
    }
}

public class ShapeTest {
    public static void main(String[] args) {
				Shape s1 = new Shape();
        Shape s2 = new Rectangle();

        s1.draw();
        s2.draw();
    }
}
//Shape Draw
//Rectangle Draw

도형 면적 계산하기

package ex06.MyExample;

class Shape02 {
    public double getArea() {
        return 0;
    }

    public Shape02() {
        super();
    }
}

class Rectangle02 extends Shape02 {
    private double width, height;

    public Rectangle02(double width, double height) {
        super();
        this.width = width;
        this.height = height;
    }

    public double getArea() {
        return width * height;
    }
}

class Triangle02 extends Shape02 {
    private double base, height;

    public double getArea() {
        return 0.5 * base * height;
    }

    public Triangle02(double base, double height) {
        super();
        this.base = base;
        this.height = height;
    }
}
public class ShapeAreaTest {
    public static void main(String[] args) {
        Shape02 obj1 = new Rectangle02(10.0, 20.0);
        Shape02 obj2 = new Triangle02(10.0, 20.0);

        System.out.println("Rectangle02: " + obj1.getArea());
        System.out.println("Triangle02: " + obj2.getArea());
    }
}
//Rectangle02: 200.0
//Triangle02: 100.0

동물 다형성

package ex06.MyExample;

class Animal {
    void speak() {
        System.out.println("Animal 클래스의 sound()");
    }
}

class Dog extends Animal {
    void speak() {
        System.out.println("멍멍");
    }
}

class Cat extends Animal {
    void speak() {
        System.out.println("야옹");
    }
}

public class DynamicCallTest {
    public static void main(String[] args) {
       Animal a1 = new Dog(); 
       Animal a2 = new Cat(); 
       
       a1.speak();
       a2.speak();
    }
}

has - a 관계

객체 안에 하나라도 다른 객체가 포함되면 has - a 관계라고 할 수 있음

package ex06.MyExample;

class Date {
    private int year, month, date;

    public Date(int year, int month, int date) {
        this.year = year;
        this.month = month;
        this.date = date;
    }

    @Override
    public String toString() {
        return "Date{" +
                "year=" + year +
                ", month=" + month +
                ", date=" + date +
                '}';
    }
}

class Employee {
    private String name;
    private Date birthDate;

    public Employee(String name, Date birthDate) {
        this.name = name;
        this.birthDate = birthDate;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\\'' +
                ", birthDate=" + birthDate +
                '}';
    }
}

public class EmployeeTest {
    public static void main(String[] args) {
        Date birth = new Date(1990, 1, 1);
        Employee employee = new Employee("홍길동", birth);
        System.out.println(employee);
    }
}
//Employee{name='홍길동', birthDate=Date{year=1990, month=1, date=1}}