-
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
2 changed files
with
52 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,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 인스턴스 아님"); | ||
} | ||
} | ||
} |
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,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 인스턴스 아님"); | ||
} | ||
} | ||
} |