Skip to content

Commit

Permalink
[Week3][Chap10] 다형성과 메서드 오버라이딩 (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
toychip committed Feb 13, 2024
1 parent 19abd30 commit cc5f590
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
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();
}
}
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의 메서드 호출, 오버라이딩된 메서드가 항상 우선권을 가진다.
}
}
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");
}
}

0 comments on commit cc5f590

Please sign in to comment.