Skip to content

Commit

Permalink
[Week3][Chap10] 다형성 시작 (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
arinming committed Jan 31, 2024
1 parent b10932e commit e1e04b0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
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();


}
}

0 comments on commit e1e04b0

Please sign in to comment.