-
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
53 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
임준형/java-basic/src/main/java/week3/poly/basic/CastingMain5.java
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,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 인스턴스 아님"); | ||
} | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
임준형/java-basic/src/main/java/week3/poly/basic/CastingMain6.java
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 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 인스턴스 아님"); | ||
} | ||
} | ||
|
||
} |