-
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.
[Week3][Chap10] 다형성과 메서드 오버라이딩 (#26)
- Loading branch information
Showing
3 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
임준형/java-basic/src/main/java/week3/poly/overriding/Child.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,12 @@ | ||
package week3.poly.overriding; | ||
|
||
public class Child extends Parent { | ||
|
||
public String value = "child"; | ||
|
||
@Override | ||
public void method() { | ||
System.out.println("Child.method"); | ||
// super.method(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
임준형/java-basic/src/main/java/week3/poly/overriding/OverridingMain.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,25 @@ | ||
package week3.poly.overriding; | ||
|
||
public class OverridingMain { | ||
|
||
public static void main(String[] args) { | ||
//자식 변수가 자식 인스턴스 참조 | ||
Child child = new Child(); | ||
System.out.println("Child -> Child"); | ||
System.out.println("value = " + child.value); | ||
child.method(); | ||
|
||
System.out.println("------------"); | ||
Parent parent = new Parent(); | ||
System.out.println("Parent -> Parent"); | ||
System.out.println("parent.value = " + parent.value); | ||
parent.method(); | ||
|
||
System.out.println("------------"); | ||
// 부모 변수가 자식 인스턴스 참조(다형적 참조) | ||
Parent poly = new Child(); | ||
System.out.println("Parent -> Child 참조"); | ||
System.out.println("value = " + poly.value); // parent, 변수는 오버라이딩이되지 않음 | ||
poly.method(); // 메서드 오버라이딩, Child의 메서드 호출, 오버라이딩된 메서드가 항상 우선권을 가진다. | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
임준형/java-basic/src/main/java/week3/poly/overriding/Parent.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,9 @@ | ||
package week3.poly.overriding; | ||
|
||
public class Parent { | ||
public String value = "parent"; | ||
|
||
public void method() { | ||
System.out.println("Parent.method"); | ||
} | ||
} |