From 851848287d650bdab00d6745f07e7c625c525c08 Mon Sep 17 00:00:00 2001 From: toychip <109949924+toychip@users.noreply.github.com> Date: Wed, 14 Feb 2024 15:04:31 +0900 Subject: [PATCH] [Week3][Chap10~13] Limjunhyoung (#31) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [Week1][Chap1] 프로젝트 환경 구성 (#2) * [Week1][Chap1] 클래스가 필요한 이유 (#2) * [Week1][Chap1] 클래스 도입 (#2) * [Week1][Chap1] 배열 도입 - 시작 (#2) * [Week1][Chap1] 배열 도입 - 리펙토링 (#2) * [Week1][Chap1] 문제와 풀이 (#2) * [Week1][Chap2] 기본형 vs 참조형2 - 변수 대입 (#2) * [Week1][Chap2] 기본형 vs 참조형3 - 메서드 호출 (#2) * [Week1][Chap2] 참조형과 메서드 호출 - 활용 (#2) * [Week1][Chap2] 변수와 초기화 (#2) * [Week1][Chap2] null, NPE (#2) * [Week1][Chap2] 문제와 풀이 (#2) * [Week1][Chap3] 절자 지향 프로그래밍1 - 시작 (#2) * [Week1][Chap3] 절자 지향 프로그래밍2 - 데이터 묶음 (#2) * [Week1][Chap3] 절자 지향 프로그래밍3 - 메서드 추출 (#2) * [Week1][Chap3] 클래스와 메서드 (#2) * [Week1][Chap3] 객체 지향 프로그래밍 (#2) * [Week1][Chap3] 문제와 풀이 (#2) * [Week1][Chap4] 생성자 - 필요한 이유 (#2) * [Week1][Chap4] this (#2) * [Week1][Chap4] 생성자 - 도입 (#2) * [Week1][Chap4] 기본 생성자 (#2) * [Week1][Chap4] 생성자 - 오버로딩과 this() (#2) * [Week1][Chap4] 문제와 풀이 (#2) * [Week1][Chap5] 패키지 시작 (#2) * [Week1][Chap5] 패키지 import, 패키지 규칙 (#2) * [Week1][Chap5] 패키지 import, 패키지 활용 (#2) * [Week2][Chap-] 폴더 구조 주차별 분할 (#14) * [Week2][Chap6] 접근 제어자 이해와 사용 (#14) * [Week2][Chap6] 캡슐화 (#14) * [Week2][Chap7] 스택 영역과 힙 영역 (#14) * [Week2][Chap7] static 변수 1~3 (#14) * [Week2][Chap7] static 메서드 1 (#14) * [Week2][Chap7] static 메서드 2 (#14) instance와 static의 상호관계 이해 * [Week2][Chap8] final 변수와 상수 1 (#14) * [Week2][Chap8] final 변수와 상수 2 (#14) * [Week2][Chap8] final 변수와 참조 (#14) * [Week2][Chap8] final1 폴더 week2 안으로 이동 (#14) * [Week2][Chap9] 상속 - 시작 (#14) * [Week2][Chap9] 상속 관계 (#14) * [Week2][Chap9] 상속과 기능 추가 (#14) * [Week2][Chap9] 상속과 메서드 오버라이딩 (#14) * [Week2][Chap9] 상속과 접근 제어 (#14) * [Week2][Chap9] super - 부모 참조 (#14) * [Week2][Chap9] super - 생성자 (#14) * [Week3][Chap10] 다형성 시작 (#14) * [Week3][Chap10] 다형성과 캐스팅 (#26) * [Week3][Chap10] 캐스팅 종류 - 업캐스팅 시작 (#26) * [Week3][Chap10] 다운캐스팅과 주의점 (#26) * [Week3][Chap10] instanceof (#26) * [Week3][Chap10] 다형성과 메서드 오버라이딩 (#26) * [Week3][Chap11] 다형성 활용1 (#26) * [Week3][Chap11] 다형성 활용2 (#26) * [Week3][Chap11] 다형성 활용3 (#26) * [Week3][Chap11] 추상 클래스1 (#26) * [Week3][Chap11] 추상 클래스2 (#26) * [Week3][Chap11] 인터페이스 다중구현 - Diamond 문제 (#26) * [Week3][Chap11] 클래스와 인터페이스 활용 (#26) * [Week3][Chap11] 다형성 - 역할과 구현 예제1 (#26) * [Week3][Chap11] 다형성 - 역할과 구현 예제2 (#26) * [Week3][Chap11] 다형성 - 역할과 구현 예제3 (#26) * [Week3][Chap11] OCP(Open-Closed Principle) 원칙 (#26) --- .../main/java/week2/extends1/ex3/Car.java" | 1 + .../java/week3/poly/basic/CastingMain1.java" | 14 ++++++++ .../java/week3/poly/basic/CastingMain2.java" | 17 +++++++++ .../java/week3/poly/basic/CastingMain3.java" | 15 ++++++++ .../java/week3/poly/basic/CastingMain4.java" | 16 +++++++++ .../java/week3/poly/basic/CastingMain5.java" | 27 ++++++++++++++ .../java/week3/poly/basic/CastingMain6.java" | 26 ++++++++++++++ .../main/java/week3/poly/basic/Child.java" | 8 +++++ .../main/java/week3/poly/basic/Parent.java" | 8 +++++ .../main/java/week3/poly/basic/PolyMain.java" | 21 +++++++++++ .../main/java/week3/poly/car0/CarMain0.java" | 17 +++++++++ .../main/java/week3/poly/car0/Driver.java" | 29 +++++++++++++++ .../src/main/java/week3/poly/car0/K3Car.java" | 16 +++++++++ .../main/java/week3/poly/car0/Model3Car.java" | 16 +++++++++ .../src/main/java/week3/poly/car1/Car.java" | 7 ++++ .../main/java/week3/poly/car1/CarMain1.java" | 22 ++++++++++++ .../main/java/week3/poly/car1/Driver.java" | 18 ++++++++++ .../src/main/java/week3/poly/car1/K3Car.java" | 18 ++++++++++ .../main/java/week3/poly/car1/Model3Car.java" | 18 ++++++++++ .../main/java/week3/poly/car1/NewCar.java" | 18 ++++++++++ .../java/week3/poly/ex1/AnimalSoundMain.java" | 28 +++++++++++++++ .../src/main/java/week3/poly/ex1/Cat.java" | 7 ++++ .../src/main/java/week3/poly/ex1/Caw.java" | 7 ++++ .../src/main/java/week3/poly/ex1/Dog.java" | 7 ++++ .../src/main/java/week3/poly/ex2/Animal.java" | 7 ++++ .../week3/poly/ex2/AnimalSoundMain1.java" | 35 +++++++++++++++++++ .../week3/poly/ex2/AnimalSoundMain2.java" | 21 +++++++++++ .../week3/poly/ex2/AnimalSoundMain3.java" | 17 +++++++++ .../src/main/java/week3/poly/ex2/Cat.java" | 9 +++++ .../src/main/java/week3/poly/ex2/Caw.java" | 8 +++++ .../src/main/java/week3/poly/ex2/Dog.java" | 8 +++++ .../src/main/java/week3/poly/ex2/Pig.java" | 4 +++ .../java/week3/poly/ex3/AbstractAnimal.java" | 9 +++++ .../java/week3/poly/ex3/AbstractMain.java" | 23 ++++++++++++ .../src/main/java/week3/poly/ex3/Cat.java" | 9 +++++ .../src/main/java/week3/poly/ex3/Caw.java" | 9 +++++ .../src/main/java/week3/poly/ex3/Dog.java" | 9 +++++ .../java/week3/poly/ex4/AbstractAnimal.java" | 7 ++++ .../java/week3/poly/ex4/AbstractMain.java" | 32 +++++++++++++++++ .../src/main/java/week3/poly/ex4/Cat.java" | 14 ++++++++ .../src/main/java/week3/poly/ex4/Caw.java" | 14 ++++++++ .../src/main/java/week3/poly/ex4/Dog.java" | 14 ++++++++ .../java/week3/poly/ex5/diamond/Child.java" | 18 ++++++++++ .../week3/poly/ex5/diamond/DiamondMain.java" | 13 +++++++ .../week3/poly/ex5/diamond/InterfaceA.java" | 6 ++++ .../week3/poly/ex5/diamond/InterfaceB.java" | 6 ++++ .../java/week3/poly/ex6/AbstractAnimal.java" | 9 +++++ .../src/main/java/week3/poly/ex6/Bird.java" | 13 +++++++ .../main/java/week3/poly/ex6/Chicken.java" | 13 +++++++ .../src/main/java/week3/poly/ex6/Dog.java" | 8 +++++ .../src/main/java/week3/poly/ex6/Fly.java" | 5 +++ .../java/week3/poly/ex6/SoundFlyMain.java" | 32 +++++++++++++++++ .../java/week3/poly/overriding/Child.java" | 12 +++++++ .../poly/overriding/OverridingMain.java" | 25 +++++++++++++ .../java/week3/poly/overriding/Parent.java" | 9 +++++ 55 files changed, 799 insertions(+) create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain1.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain2.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain3.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain4.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain5.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain6.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/Child.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/Parent.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/PolyMain.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car0/CarMain0.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car0/Driver.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car0/K3Car.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car0/Model3Car.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/Car.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/CarMain1.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/Driver.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/K3Car.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/Model3Car.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/NewCar.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex1/AnimalSoundMain.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex1/Cat.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex1/Caw.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex1/Dog.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/Animal.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/AnimalSoundMain1.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/AnimalSoundMain2.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/AnimalSoundMain3.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/Cat.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/Caw.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/Dog.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/Pig.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex3/AbstractAnimal.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex3/AbstractMain.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex3/Cat.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex3/Caw.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex3/Dog.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex4/AbstractAnimal.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex4/AbstractMain.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex4/Cat.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex4/Caw.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex4/Dog.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex5/diamond/Child.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex5/diamond/DiamondMain.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex5/diamond/InterfaceA.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex5/diamond/InterfaceB.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/AbstractAnimal.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/Bird.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/Chicken.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/Dog.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/Fly.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/SoundFlyMain.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/Child.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/OverridingMain.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/Parent.java" diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week2/extends1/ex3/Car.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week2/extends1/ex3/Car.java" index 87fcf67..a42c6e3 100644 --- "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week2/extends1/ex3/Car.java" +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week2/extends1/ex3/Car.java" @@ -1,5 +1,6 @@ package week2.extends1.ex3; + public class Car { public void move() { System.out.println("차를 이동합니다."); diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain1.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain1.java" new file mode 100644 index 0000000..6c0ed95 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain1.java" @@ -0,0 +1,14 @@ +package week3.poly.basic; + +public class CastingMain1 { + public static void main(String[] args) { + // 부모 변수가 자식 인스턴스 참조(다형적 참조) + Parent poly = new Child(); // x001 + // 단 자식의 기능은 호출할 수 없다. + // poly.childMethod() // compile error!! + + // 다운 캐스팅(부모 타입 -> 자식 타입) + Child child = (Child) poly; // x001 + child.childMethod(); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain2.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain2.java" new file mode 100644 index 0000000..b85ce74 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain2.java" @@ -0,0 +1,17 @@ +package week3.poly.basic; + +public class CastingMain2 { + public static void main(String[] args) { + // 부모 변수가 자식 인스턴스 참조(다형적 참조) + Parent poly = new Child(); // x001 + // 단 자식의 기능은 호출할 수 없다. + // poly.childMethod() // compile error!! + + // 다운 캐스팅(부모 타입 -> 자식 타입) + Child child = (Child) poly; // x001 + child.childMethod(); + + // 일시적 다운 캐스팅 - 해당 메서드를 호출하는 순간만 다운캐스팅 + ((Child) poly).childMethod(); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain3.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain3.java" new file mode 100644 index 0000000..14cb10e --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain3.java" @@ -0,0 +1,15 @@ +package week3.poly.basic; + +// upcasting vs downcasting +public class CastingMain3 { + + public static void main(String[] args) { + Child child = new Child(); + Parent parent1 = (Parent) child; // 업캐스팅 생략 가능, 생략 권장 + Parent parent2 = child; // 업캐스팅 생략 + + parent1.parentMethod(); + parent2.parentMethod(); + } + +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain4.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain4.java" new file mode 100644 index 0000000..ebdd5a4 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain4.java" @@ -0,0 +1,16 @@ +package week3.poly.basic; + +// 다운캐스팅을 자동으로 하지 않는 이유 +public class CastingMain4 { + + public static void main(String[] args) { + Parent parent1 = new Child(); + Child child1 = (Child) parent1; + child1.childMethod(); // 문제 x + + Parent parent2 = new Parent(); + Child child2 = (Child) parent2; // 런타임 오류 캐스팅 + child2.childMethod(); // 실행 불가 + } + +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain5.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain5.java" new file mode 100644 index 0000000..890fe0d --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain5.java" @@ -0,0 +1,27 @@ +package week3.poly.basic; + +// 다운캐스팅을 자동으로 하지 않는 이유 +public class CastingMain5 { + + public static void main(String[] args) { + Parent parent1 = new Parent(); + call(parent1); + System.out.println("-------------"); + + Parent parent2 = new Child(); + call(parent2); + } + + private static void call(Parent parent) { + parent.parentMethod(); + + if (parent instanceof Child) { + System.out.println("Child 인스턴스 맞음"); + Child child = (Child) parent; + child.childMethod(); + } else { + System.out.println("Child 인스턴스 아님"); + } + } + +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain6.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain6.java" new file mode 100644 index 0000000..cf43164 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/CastingMain6.java" @@ -0,0 +1,26 @@ +package week3.poly.basic; + +// 다운캐스팅을 자동으로 하지 않는 이유 +public class CastingMain6 { + + public static void main(String[] args) { + Parent parent1 = new Parent(); + call(parent1); + System.out.println("-------------"); + + Parent parent2 = new Child(); + call(parent2); + } + + private static void call(Parent parent) { + parent.parentMethod(); + + if (parent instanceof Child child) { + System.out.println("Child 인스턴스 맞음"); + child.childMethod(); + } else { + System.out.println("Child 인스턴스 아님"); + } + } + +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/Child.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/Child.java" new file mode 100644 index 0000000..8abdf07 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/Child.java" @@ -0,0 +1,8 @@ +package week3.poly.basic; + +public class Child extends Parent { + + public void childMethod() { + System.out.println("Child.childMethod"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/Parent.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/Parent.java" new file mode 100644 index 0000000..9336ffb --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/Parent.java" @@ -0,0 +1,8 @@ +package week3.poly.basic; + +public class Parent { + + public void parentMethod() { + System.out.println("Parent.parentMethod"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/PolyMain.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/PolyMain.java" new file mode 100644 index 0000000..657de37 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/basic/PolyMain.java" @@ -0,0 +1,21 @@ +package week3.poly.basic; + +public class PolyMain { + public static void main(String[] args) { + // 부모 변수가 부모 인스턴스 참조 + System.out.println("Parent -> Parent"); + Parent parent = new Parent(); + parent.parentMethod(); + + // 자식 변수가 자식 인스턴스 참조 + System.out.println("\nChild -> Child"); + Child child = new Child(); + child.parentMethod(); + child.childMethod(); + + // 부모 변수가 자식 인스턴스 참조(다형석 참조) + System.out.println("\nParent -> Child"); + Parent poly = new Child(); + poly.parentMethod(); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car0/CarMain0.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car0/CarMain0.java" new file mode 100644 index 0000000..5deb44a --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car0/CarMain0.java" @@ -0,0 +1,17 @@ +package week3.poly.car0; + +public class CarMain0 { + + public static void main(String[] args) { + Driver driver = new Driver(); + K3Car k3Car = new K3Car(); + driver.setK3Car(k3Car); + driver.drive(); + + // 추가 + Model3Car model3Car = new Model3Car(); + driver.setK3Car(null); + driver.setModel3Car(model3Car); + driver.drive(); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car0/Driver.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car0/Driver.java" new file mode 100644 index 0000000..281d06f --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car0/Driver.java" @@ -0,0 +1,29 @@ +package week3.poly.car0; + +public class Driver { + + private K3Car k3Car; + private Model3Car model3Car; // 추가 + + public void setK3Car(K3Car k3Car) { + this.k3Car = k3Car; + } + + public void setModel3Car(Model3Car model3Car) { + this.model3Car = model3Car; + } + + public void drive() { + System.out.println("자동차를 운전합니다."); + if (k3Car != null) { + k3Car.startEngine(); + k3Car.pressAccelerator(); + k3Car.offEngine(); + } else if (model3Car != null) { + model3Car.startEngine(); + model3Car.pressAccelerator(); + model3Car.offEngine(); + } + } + +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car0/K3Car.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car0/K3Car.java" new file mode 100644 index 0000000..63deabc --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car0/K3Car.java" @@ -0,0 +1,16 @@ +package week3.poly.car0; + +public class K3Car { + + public void startEngine() { + System.out.println("K3Car.startEngine"); + } + + public void offEngine() { + System.out.println("K3Car.offEngine"); + } + + public void pressAccelerator() { + System.out.println("K3Car.pressAccelerator"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car0/Model3Car.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car0/Model3Car.java" new file mode 100644 index 0000000..b05e256 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car0/Model3Car.java" @@ -0,0 +1,16 @@ +package week3.poly.car0; + +public class Model3Car { + + public void startEngine() { + System.out.println("Model3Car.startEngine"); + } + + public void offEngine() { + System.out.println("Model3Car.offEngine"); + } + + public void pressAccelerator() { + System.out.println("Model3Car.pressAccelerator"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/Car.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/Car.java" new file mode 100644 index 0000000..840c414 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/Car.java" @@ -0,0 +1,7 @@ +package week3.poly.car1; + +public interface Car { + void startEngine(); + void offEngine(); + void pressAccelerator(); +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/CarMain1.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/CarMain1.java" new file mode 100644 index 0000000..8461b71 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/CarMain1.java" @@ -0,0 +1,22 @@ +package week3.poly.car1; + +public class CarMain1 { + public static void main(String[] args) { + Driver driver = new Driver(); + + // k3 선택 + K3Car k3Car = new K3Car(); + driver.setCar(k3Car); + driver.drive(); + + // 차량 변경(k3 -> model3) + Model3Car model3Car = new Model3Car(); + driver.setCar(model3Car); + driver.drive(); + + //차량 변경(model3 -> newCar) + NewCar newCar = new NewCar(); + driver.setCar(newCar); + driver.drive(); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/Driver.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/Driver.java" new file mode 100644 index 0000000..4fe643f --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/Driver.java" @@ -0,0 +1,18 @@ +package week3.poly.car1; + +public class Driver { + + private Car car; + + public void setCar(Car car) { + System.out.println("자동차를 설정합니다: " + car); + this.car = car; + } + + public void drive() { + System.out.println("자동차를 운전합니다."); + car.startEngine(); + car.pressAccelerator(); + car.offEngine(); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/K3Car.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/K3Car.java" new file mode 100644 index 0000000..e12a51e --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/K3Car.java" @@ -0,0 +1,18 @@ +package week3.poly.car1; + +public class K3Car implements Car { + @Override + public void startEngine() { + System.out.println("K3Car.startEngine"); + } + + @Override + public void offEngine() { + System.out.println("K3Car.offEngine"); + } + + @Override + public void pressAccelerator() { + System.out.println("K3Car.pressAccelerator"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/Model3Car.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/Model3Car.java" new file mode 100644 index 0000000..904ea85 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/Model3Car.java" @@ -0,0 +1,18 @@ +package week3.poly.car1; + +public class Model3Car implements Car { + @Override + public void startEngine() { + System.out.println("Model3Car.startEngine"); + } + + @Override + public void offEngine() { + System.out.println("Model3Car.offEngine"); + } + + @Override + public void pressAccelerator() { + System.out.println("Model3Car.pressAccelerator"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/NewCar.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/NewCar.java" new file mode 100644 index 0000000..354b27d --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/car1/NewCar.java" @@ -0,0 +1,18 @@ +package week3.poly.car1; + +public class NewCar implements Car { + @Override + public void startEngine() { + System.out.println("NewCar.startEngine"); + } + + @Override + public void offEngine() { + System.out.println("NewCar.offEngine"); + } + + @Override + public void pressAccelerator() { + System.out.println("NewCar.pressAccelerator"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex1/AnimalSoundMain.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex1/AnimalSoundMain.java" new file mode 100644 index 0000000..010f8b7 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex1/AnimalSoundMain.java" @@ -0,0 +1,28 @@ +package week3.poly.ex1; + +public class AnimalSoundMain { + public static void main(String[] args) { + Dog dog = new Dog(); + Cat cat = new Cat(); + Caw caw = new Caw(); + + System.out.println("동물 소리 테스트 시작"); + dog.sound(); + System.out.println("동물 소리 테스트 종료"); + + soundCat(cat); + soundCaw(caw); + } + + private static void soundCat(final Cat cat) { + System.out.println("동물 소리 테스트 시작"); + cat.sound(); + System.out.println("동물 소리 테스트 종료"); + } + + private static void soundCaw(Caw caw) { + System.out.println("동물 소리 테스트 시작"); + caw.sound(); + System.out.println("동물 소리 테스트 종료"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex1/Cat.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex1/Cat.java" new file mode 100644 index 0000000..1eaa3b0 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex1/Cat.java" @@ -0,0 +1,7 @@ +package week3.poly.ex1; + +public class Cat { + public void sound() { + System.out.println("야옹"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex1/Caw.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex1/Caw.java" new file mode 100644 index 0000000..ff73ef9 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex1/Caw.java" @@ -0,0 +1,7 @@ +package week3.poly.ex1; + +public class Caw { + public void sound() { + System.out.println("음메"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex1/Dog.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex1/Dog.java" new file mode 100644 index 0000000..9739b1d --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex1/Dog.java" @@ -0,0 +1,7 @@ +package week3.poly.ex1; + +public class Dog { + public void sound() { + System.out.println("멍멍"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/Animal.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/Animal.java" new file mode 100644 index 0000000..fa59b4a --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/Animal.java" @@ -0,0 +1,7 @@ +package week3.poly.ex2; + +public class Animal { + public void sound() { + System.out.println("동물 울음 소리"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/AnimalSoundMain1.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/AnimalSoundMain1.java" new file mode 100644 index 0000000..fce9ac5 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/AnimalSoundMain1.java" @@ -0,0 +1,35 @@ +package week3.poly.ex2; + +import java.util.ArrayList; +import java.util.List; + +public class AnimalSoundMain1 { + public static void main(String[] args) { + Dog dog = new Dog(); + Cat cat = new Cat(); + Caw caw = new Caw(); + + soundAnimal(dog); + soundAnimal(cat); + soundAnimal(caw); + + // 혼자해본 연습 + /* + Animal dog1 = new Dog(); + Animal cat1 = new Cat(); + Animal caw1 = new Caw(); + List animals = new ArrayList<>(); + animals.add(dog1); + animals.add(cat1); + animals.add(caw1); + + animals.forEach(animal -> soundAnimal(animal)); + */ + } + + private static void soundAnimal(Animal animal) { + System.out.println("동물 소리 테스트 시작"); + animal.sound(); + System.out.println("동물 소리 테스트 종료"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/AnimalSoundMain2.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/AnimalSoundMain2.java" new file mode 100644 index 0000000..300c142 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/AnimalSoundMain2.java" @@ -0,0 +1,21 @@ +package week3.poly.ex2; + +public class AnimalSoundMain2 { + public static void main(String[] args) { + Dog dog = new Dog(); + Cat cat = new Cat(); + Caw caw = new Caw(); + + Animal[] animals = {dog, cat, caw}; + + for (Animal animal : animals) { + soundAnimal(animal); + } + } + + private static void soundAnimal(Animal animal) { + System.out.println("동물 소리 테스트 시작"); + animal.sound(); + System.out.println("동물 소리 테스트 종료"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/AnimalSoundMain3.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/AnimalSoundMain3.java" new file mode 100644 index 0000000..3f55c07 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/AnimalSoundMain3.java" @@ -0,0 +1,17 @@ +package week3.poly.ex2; + +import java.util.Arrays; + +public class AnimalSoundMain3 { + public static void main(String[] args) { + Animal[] animals = {new Dog(), new Cat(), new Caw()}; + + Arrays.stream(animals).toList().forEach(AnimalSoundMain3::soundAnimal); + } + + private static void soundAnimal(Animal animal) { + System.out.println("동물 소리 테스트 시작"); + animal.sound(); + System.out.println("동물 소리 테스트 종료"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/Cat.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/Cat.java" new file mode 100644 index 0000000..da2235c --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/Cat.java" @@ -0,0 +1,9 @@ +package week3.poly.ex2; + +public class Cat extends Animal { + + @Override + public void sound() { + System.out.println("야옹"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/Caw.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/Caw.java" new file mode 100644 index 0000000..a7e2b6d --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/Caw.java" @@ -0,0 +1,8 @@ +package week3.poly.ex2; + +public class Caw extends Animal{ + @Override + public void sound() { + System.out.println("음메"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/Dog.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/Dog.java" new file mode 100644 index 0000000..71adaaa --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/Dog.java" @@ -0,0 +1,8 @@ +package week3.poly.ex2; + +public class Dog extends Animal{ + @Override + public void sound() { + System.out.println("멍멍"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/Pig.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/Pig.java" new file mode 100644 index 0000000..c3c6461 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex2/Pig.java" @@ -0,0 +1,4 @@ +package week3.poly.ex2; + +public class Pig { +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex3/AbstractAnimal.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex3/AbstractAnimal.java" new file mode 100644 index 0000000..ed05fa6 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex3/AbstractAnimal.java" @@ -0,0 +1,9 @@ +package week3.poly.ex3; + +public abstract class AbstractAnimal { + + public abstract void sound(); + public void move(){ + System.out.println("동물이 움직입니다."); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex3/AbstractMain.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex3/AbstractMain.java" new file mode 100644 index 0000000..22fae96 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex3/AbstractMain.java" @@ -0,0 +1,23 @@ +package week3.poly.ex3; + +public class AbstractMain { + + public static void main(String[] args) { + Dog dog = new Dog(); + Cat cat = new Cat(); + Caw caw = new Caw(); + + cat.sound(); + cat.move(); + + soundAnimal(dog); + soundAnimal(cat); + soundAnimal(caw); + } + + private static void soundAnimal(AbstractAnimal animal) { + System.out.println("테스트 시작"); + animal.sound(); + System.out.println("테스트 종료"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex3/Cat.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex3/Cat.java" new file mode 100644 index 0000000..38a1b8b --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex3/Cat.java" @@ -0,0 +1,9 @@ +package week3.poly.ex3; + +public class Cat extends AbstractAnimal { + + @Override + public void sound() { + System.out.println("야옹"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex3/Caw.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex3/Caw.java" new file mode 100644 index 0000000..a705eb6 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex3/Caw.java" @@ -0,0 +1,9 @@ +package week3.poly.ex3; + + +public class Caw extends AbstractAnimal { + @Override + public void sound() { + System.out.println("음메"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex3/Dog.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex3/Dog.java" new file mode 100644 index 0000000..a3daa2c --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex3/Dog.java" @@ -0,0 +1,9 @@ +package week3.poly.ex3; + + +public class Dog extends AbstractAnimal { + @Override + public void sound() { + System.out.println("멍멍"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex4/AbstractAnimal.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex4/AbstractAnimal.java" new file mode 100644 index 0000000..c4a475a --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex4/AbstractAnimal.java" @@ -0,0 +1,7 @@ +package week3.poly.ex4; + +public abstract class AbstractAnimal { + + public abstract void sound(); + public abstract void move(); +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex4/AbstractMain.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex4/AbstractMain.java" new file mode 100644 index 0000000..16dcc8b --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex4/AbstractMain.java" @@ -0,0 +1,32 @@ +package week3.poly.ex4; + +public class AbstractMain { + + public static void main(String[] args) { + Dog dog = new Dog(); + Cat cat = new Cat(); + Caw caw = new Caw(); + + cat.sound(); + cat.move(); + + soundAnimal(dog); + moveAnimal(dog); + + soundAnimal(cat); + moveAnimal(cat); + + soundAnimal(caw); + moveAnimal(caw); + } + + private static void soundAnimal(AbstractAnimal animal) { + System.out.println("테스트 시작"); + animal.sound(); + System.out.println("테스트 종료"); + } + + private static void moveAnimal(AbstractAnimal animal) { + animal.move(); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex4/Cat.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex4/Cat.java" new file mode 100644 index 0000000..8bcc93e --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex4/Cat.java" @@ -0,0 +1,14 @@ +package week3.poly.ex4; + +public class Cat extends AbstractAnimal { + + @Override + public void sound() { + System.out.println("야옹"); + } + + @Override + public void move() { + System.out.println("고양이 이동"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex4/Caw.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex4/Caw.java" new file mode 100644 index 0000000..0967bf6 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex4/Caw.java" @@ -0,0 +1,14 @@ +package week3.poly.ex4; + + +public class Caw extends AbstractAnimal { + @Override + public void sound() { + System.out.println("음메"); + } + + @Override + public void move() { + System.out.println("소 이동"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex4/Dog.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex4/Dog.java" new file mode 100644 index 0000000..5a284f2 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex4/Dog.java" @@ -0,0 +1,14 @@ +package week3.poly.ex4; + + +public class Dog extends AbstractAnimal { + @Override + public void sound() { + System.out.println("멍멍"); + } + + @Override + public void move() { + System.out.println("개 이동"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex5/diamond/Child.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex5/diamond/Child.java" new file mode 100644 index 0000000..ac90911 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex5/diamond/Child.java" @@ -0,0 +1,18 @@ +package week3.poly.ex5.diamond; + +public class Child implements InterfaceA, InterfaceB{ + @Override + public void methodA() { + System.out.println("Child.methodA"); + } + + @Override + public void methodB() { + System.out.println("Child.methodB"); + } + + @Override + public void methodCommon() { + System.out.println("Child.methodCommon"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex5/diamond/DiamondMain.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex5/diamond/DiamondMain.java" new file mode 100644 index 0000000..ddfff39 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex5/diamond/DiamondMain.java" @@ -0,0 +1,13 @@ +package week3.poly.ex5.diamond; + +public class DiamondMain { + public static void main(String[] args) { + InterfaceA a = new Child(); + a.methodA(); + a.methodCommon(); + + InterfaceB b = new Child(); + b.methodB(); + b.methodCommon(); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex5/diamond/InterfaceA.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex5/diamond/InterfaceA.java" new file mode 100644 index 0000000..8bc7ca6 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex5/diamond/InterfaceA.java" @@ -0,0 +1,6 @@ +package week3.poly.ex5.diamond; + +public interface InterfaceA { + void methodA(); + void methodCommon(); +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex5/diamond/InterfaceB.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex5/diamond/InterfaceB.java" new file mode 100644 index 0000000..a2f9bbc --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex5/diamond/InterfaceB.java" @@ -0,0 +1,6 @@ +package week3.poly.ex5.diamond; + +public interface InterfaceB { + void methodB(); + void methodCommon(); +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/AbstractAnimal.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/AbstractAnimal.java" new file mode 100644 index 0000000..7ed07e0 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/AbstractAnimal.java" @@ -0,0 +1,9 @@ +package week3.poly.ex6; + +public abstract class AbstractAnimal { + public abstract void sound(); + + public void move() { + System.out.println("동물이 이동합니다."); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/Bird.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/Bird.java" new file mode 100644 index 0000000..9d24096 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/Bird.java" @@ -0,0 +1,13 @@ +package week3.poly.ex6; + +public class Bird extends AbstractAnimal implements Fly { + @Override // AbstractAnimal + public void sound() { + System.out.println("짹짹"); + } + + @Override + public void fly() { + System.out.println("새 날기"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/Chicken.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/Chicken.java" new file mode 100644 index 0000000..95a9d39 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/Chicken.java" @@ -0,0 +1,13 @@ +package week3.poly.ex6; + +public class Chicken extends AbstractAnimal implements Fly { + @Override + public void sound() { + System.out.println("꼬기오"); + } + + @Override + public void fly() { + System.out.println("닭 날기"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/Dog.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/Dog.java" new file mode 100644 index 0000000..0b48818 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/Dog.java" @@ -0,0 +1,8 @@ +package week3.poly.ex6; + +public class Dog extends AbstractAnimal { + @Override + public void sound() { + System.out.println("멍멍"); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/Fly.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/Fly.java" new file mode 100644 index 0000000..b141b6b --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/Fly.java" @@ -0,0 +1,5 @@ +package week3.poly.ex6; + +public interface Fly { + void fly(); +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/SoundFlyMain.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/SoundFlyMain.java" new file mode 100644 index 0000000..67634e2 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/ex6/SoundFlyMain.java" @@ -0,0 +1,32 @@ +package week3.poly.ex6; + +public class SoundFlyMain { + + public static void main(String[] args) { + Dog dog = new Dog(); + Bird bird = new Bird(); + Chicken chicken = new Chicken(); + + soundAnimal(dog); + soundAnimal(bird); + soundAnimal(chicken); + + flyAnimal(bird); + flyAnimal(chicken); + } + + //AbstractAnimal 사용 가능 + private static void soundAnimal(AbstractAnimal animal) { + System.out.println("동물 소리 테스트 시작"); + animal.sound(); + System.out.println("동물 소리 테스트 종료"); + } + + //Fly 인터페이스가 있으면 사용 가능 + private static void flyAnimal(Fly fly) { + System.out.println("날기 테스트 시작"); + fly.fly(); + System.out.println("날기 테스트 종료"); + } + +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/Child.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/Child.java" new file mode 100644 index 0000000..f92f019 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/Child.java" @@ -0,0 +1,12 @@ +package week3.poly.overriding; + +public class Child extends Parent { + + public String value = "child"; + + @Override + public void method() { + System.out.println("Child.method"); +// super.method(); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/OverridingMain.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/OverridingMain.java" new file mode 100644 index 0000000..4a27f2d --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/OverridingMain.java" @@ -0,0 +1,25 @@ +package week3.poly.overriding; + +public class OverridingMain { + + public static void main(String[] args) { + //자식 변수가 자식 인스턴스 참조 + Child child = new Child(); + System.out.println("Child -> Child"); + System.out.println("value = " + child.value); + child.method(); + + System.out.println("------------"); + Parent parent = new Parent(); + System.out.println("Parent -> Parent"); + System.out.println("parent.value = " + parent.value); + parent.method(); + + System.out.println("------------"); + // 부모 변수가 자식 인스턴스 참조(다형적 참조) + Parent poly = new Child(); + System.out.println("Parent -> Child 참조"); + System.out.println("value = " + poly.value); // parent, 변수는 오버라이딩이되지 않음 + poly.method(); // 메서드 오버라이딩, Child의 메서드 호출, 오버라이딩된 메서드가 항상 우선권을 가진다. + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/Parent.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/Parent.java" new file mode 100644 index 0000000..238ea94 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/Parent.java" @@ -0,0 +1,9 @@ +package week3.poly.overriding; + +public class Parent { + public String value = "parent"; + + public void method() { + System.out.println("Parent.method"); + } +}