Skip to content

Commit

Permalink
[Week3][Chap10~13] Kimarin (#32)
Browse files Browse the repository at this point in the history
* [Week1][Chap1] 프로젝트 환경 구성 (#4)

* [Week1][Chap1] 클래스가 필요한 이유 (#4)

* [Week1][Chap1] 클래스 도입 (#4)

* [Week1][Chap1] 배열 도입 - 시작 (#4)

* [Week1][Chap1] 배열 도입 - 리펙토링 (#4)

* [Week1][Chap1] 문제와 풀이 (#4)

* [Week1][Chap2] 기본형 vs 참조형2 - 변수 대입 (#4)

* [Week1][Chap2] 기본형 vs 참조형3 - 메서드 호출 (#4)

* [Week1][Chap2] 참조형과 메서드 호출 - 활용 (#4)

* [Week1][Chap2] 변수와 초기화 (#4)

* [Week1][Chap2] null (#4)

* [Week1][Chap2] NullPointerException (#4)

* [Week1][Chap2] 문제와 풀이 (#4)

* [Week1][Chap3] 절차 지향 프로그래밍1 - 시작 (#4)

* [Week1][Chap3] 절차 지향 프로그래밍2 - 데이터 묶음 (#4)

* [Week1][Chap3] 절차 지향 프로그래밍3 - 메서드 추출 (#4)

* [Week1][Chap3] 클래스와 메서드 (#4)

* [Week1][Chap3] 객체 지향 프로그래밍 (#4)

* [Week1][Chap3] 문제와 풀이 (#4)

* [Week1][Chap4] 생성자 - 필요한 이유 (#4)

* [Week1][Chap4] this (#4)

* [Week1][Chap4] 생성자 - 도입 (#4)

* [Week1][Chap4] 기본 생성자 (#4)

* [Week1][Chap4] 오버로딩과 this() (#4)

* [Week1][Chap4] 문제와 풀이 (#4)

* [Week1][Chap5] 패키지 - 시작 (#4)

* [Week1][Chap5] 패키지 - import (#4)

* [Week1][Chap5] 패키지 활용 (#4)

* [Week2][Chap6] 접근 제어자 이해1 (#15)

* [Week2][Chap6] 접근 제어자 이해2 (#15)

* [Week2][Chap6] 접근 제어자 사용 - 필드, 메서드 (#15)

* [Week2][Chap6] 접근 제어자 사용 - 클래스 레벨 (#15)

* [Week2][Chap6] 캡슐화 (#15)

* [Week2][Chap6] 문제와 풀이 (#15)

* [Week2][Chap7] 스택 영역 (#15)

* [Week2][Chap7] 스택 영역과 힙 영역 (#15)

* [Week2][Chap7] static 변수1 (#15)

* [Week2][Chap7] static 변수2 (#15)

* [Week2][Chap7] static 변수3 (#15)

* [Week2][Chap7] static 메서드1 (#15)

* [Week2][Chap7] static 메서드2 (#15)

* [Week2][Chap7] static 메서드3 (#15)

* [Week2][Chap7] 문제와 풀이 (#15)

* [Week2][Chap8] final 변수와 상수1 (#15)

* [Week2][Chap8] final 변수와 상수2 (#15)

* [Week2][Chap8] final 변수와 참조 (#15)

* [Week2][Chap8] 정리 (#15)

* [Week2][Chap9] 상속 - 시작 (#15)

* [Week2][Chap9] 상속 - 시작 (#15)

* [Week2][Chap9] 상속 관계 (#15)

* [Week2][Chap9] 상속과 기능 추가 (#15)

* [Week2][Chap9] 상속과 메서드 오버라이딩 (#15)

* [Week2][Chap9] 상속과 접근 제어 (#15)

* [Week2][Chap9] super - 부모 참조 (#15)

* [Week2][Chap9] super - 생성자 (#15)

* [Week2][Chap9] 문제와 풀이 (#15)

* [Week3][Chap10] 다형성 시작 (#27)

* [Week3][Chap10] 다형성과 캐스팅 (#27)

* [Week3][Chap10] 캐스팅의 종류 (#27)

* [Week3][Chap10] 다운캐스팅과 주의점 (#27)

* [Week3][Chap10] 다운캐스팅과 주의점 (#27)

* [Week3][Chap10] instanceof (#27)

* [Week3][Chap10] 다형성과 메서드 오버라이딩 (#27)

* [Week3][Chap11] 다형성 활용1 (#27)

* [Week3][Chap11] 다형성 활용2 (#27)

* [Week3][Chap11] 다형성 활용3 (#27)

* [Week3][Chap11] 추상 클래스2 (#27)

* [Week3][Chap11] 추상 클래스2 (#27)

* [Week3][Chap11] 인터페이스 (#27)

* [Week3][Chap11] 인터페이스 - 다중 구현 (#27)

* [Week3][Chap11] 클래스와 인터페이스 활용 (#27)

* [Week3][Chap12] 다형성 - 역할과 구현 예제1 (#27)

* [Week3][Chap12] 다형성 - 역할과 구현 예제2 (#27)

* [Week3][Chap12] 다형성 - 역할과 구현 예제3 (#27)

* [Week3][Chap12] OCP(Open-Closed Principle) 원칙 (#27)

* [Week3][Chap12] 문제와 풀이 (#27)
  • Loading branch information
arinming authored Feb 14, 2024
1 parent 8518482 commit 5920196
Show file tree
Hide file tree
Showing 78 changed files with 1,085 additions and 0 deletions.
10 changes: 10 additions & 0 deletions 김아린/java-basic/.idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions 김아린/java-basic/src/poly/basic/CastingMain1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package poly.basic;

public class CastingMain1 {
public static void main(String[] args) {
// 부모 변수가 자식 인스턴스 참조(다형적 참조)
Parent poly = new Child();

// 단, 자식의 기능은 호출 불가, 컴파일 오류 발생
// poly.childMethod();

// 다운 캐스팅(부모 타입 -> 자식 타입)
Child child = (Child) poly; // x001
child.childMethod();
}
}
19 changes: 19 additions & 0 deletions 김아린/java-basic/src/poly/basic/CastingMain2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package poly.basic;

public class CastingMain2 {
public static void main(String[] args) {
// 부모 변수가 자식 인스턴스 참조(다형적 참조)
Parent poly = new Child();

// 단, 자식의 기능은 호출 불가, 컴파일 오류 발생
// poly.childMethod();

// 다운 캐스팅(부모 타입 -> 자식 타입)
Child child = (Child) poly; // x001
child.childMethod();

// 일시적 다운 캐스팅 - 해당 메서드를 호출하는 순간만 다운 캐스팅
((Child) poly).childMethod();

}
}
13 changes: 13 additions & 0 deletions 김아린/java-basic/src/poly/basic/CastingMain3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package 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();
}
}
15 changes: 15 additions & 0 deletions 김아린/java-basic/src/poly/basic/CastingMain4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package poly.basic;

// 다운 캐스팅을 자동으로 하지 않는 이유
public class CastingMain4 {
public static void main(String[] args) {
Parent parent1 = new Child();
Child child1 = (Child) parent1;
child1.childMethod(); // 문제 없음

Parent parent2 = new Parent();

Child child2 = (Child) parent2; // 런타임 오류 - ClassCastException
child2.childMethod(); // 실행 불가
}
}
26 changes: 26 additions & 0 deletions 김아린/java-basic/src/poly/basic/CastingMain5.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package poly.basic;

public class CastingMain5 {
public static void main(String[] args) {
Parent parent1 = new Parent();
System.out.println("parent1 호출");
call(parent1);

Parent parent2 = new Child();
System.out.println("parent2 호출");
call(parent2);
}

private static void call(Parent parent) {
parent.parentMethod();

if (parent instanceof Child) {
// Child c = new Parent();
System.out.println("Child 인스턴스 맞음");
Child child = (Child) parent;
child.childMethod();
} else {
System.out.println("Child 인스턴스 아님");
}
}
}
26 changes: 26 additions & 0 deletions 김아린/java-basic/src/poly/basic/CastingMain6.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package poly.basic;

public class CastingMain6 {
public static void main(String[] args) {
Parent parent1 = new Parent();
System.out.println("parent1 호출");
call(parent1);

Parent parent2 = new Child();
System.out.println("parent2 호출");
call(parent2);
}

private static void call(Parent parent) {
parent.parentMethod();

// Child 인스턴스인 경우 childMethod() 실행
if (parent instanceof Child child) {
// Child c = new Parent();
System.out.println("Child 인스턴스 맞음");
child.childMethod();
} else {
System.out.println("Child 인스턴스 아님");
}
}
}
7 changes: 7 additions & 0 deletions 김아린/java-basic/src/poly/basic/Child.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package poly.basic;

public class Child extends Parent {
public void childMethod() {
System.out.println("Child.childMethod");
}
}
7 changes: 7 additions & 0 deletions 김아린/java-basic/src/poly/basic/Parent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package poly.basic;

public class Parent {
public void parentMethod() {
System.out.println("Parent.parentMethod");
}
}
29 changes: 29 additions & 0 deletions 김아린/java-basic/src/poly/basic/PolyMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package 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("Child -> Child");
Child child = new Child();
child.parentMethod();
child.childMethod();

// 부모 변수가 자식 인스턴스 참조 (다형적 참조)
System.out.println("Parent -> Child");
Parent poly = new Child();
// 부모 타입은 자식을 참조할 수 있다
poly.parentMethod();

// 자식은 부모를 담을 수 없다
// Child child1 = new Parent();
// 자식의 기능은 호출할 수 없다 -> 컴파일 오류 발생
// poly.childMethod();


}
}
17 changes: 17 additions & 0 deletions 김아린/java-basic/src/poly/car0/CarMain0.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package 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();
}
}
29 changes: 29 additions & 0 deletions 김아린/java-basic/src/poly/car0/Driver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package 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();
}
}
}
14 changes: 14 additions & 0 deletions 김아린/java-basic/src/poly/car0/K3Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package 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");
}
}
14 changes: 14 additions & 0 deletions 김아린/java-basic/src/poly/car0/Model3Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package 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");
}
}
9 changes: 9 additions & 0 deletions 김아린/java-basic/src/poly/car1/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package poly.car1;

public interface Car {
void startEngine();

void offEngine();

void pressAccelerator();
}
22 changes: 22 additions & 0 deletions 김아린/java-basic/src/poly/car1/CarMain1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package 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();
}
}
17 changes: 17 additions & 0 deletions 김아린/java-basic/src/poly/car1/Driver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package 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();
}
}
18 changes: 18 additions & 0 deletions 김아린/java-basic/src/poly/car1/K3Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package 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");
}
}
18 changes: 18 additions & 0 deletions 김아린/java-basic/src/poly/car1/Model3Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package 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");
}
}
18 changes: 18 additions & 0 deletions 김아린/java-basic/src/poly/car1/NewCar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package 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");
}
}
18 changes: 18 additions & 0 deletions 김아린/java-basic/src/poly/diamond/Child.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package poly.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");
}
}
16 changes: 16 additions & 0 deletions 김아린/java-basic/src/poly/diamond/DiamondMian.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package poly.diamond;

public class DiamondMian {
public static void main(String[] args) {
InterfaceA a = new Child();
a.methodA();
a.methodCommon();

InterfaceB b = new Child();
b.methodB();
b.methodCommon();

Child child = new Child();
child.methodCommon();
}
}
Loading

0 comments on commit 5920196

Please sign in to comment.