-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
|
||
} | ||
} |