From ed152fa2f1a851588b37926890016c387fc04f99 Mon Sep 17 00:00:00 2001 From: arinming Date: Thu, 1 Feb 2024 13:57:40 +0900 Subject: [PATCH] =?UTF-8?q?[Week3][Chap10]=20=EB=8B=A4=ED=98=95=EC=84=B1?= =?UTF-8?q?=EA=B3=BC=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=98=A4=EB=B2=84?= =?UTF-8?q?=EB=9D=BC=EC=9D=B4=EB=94=A9=20(#27)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/poly/overriding/Child.java" | 10 ++++++++ .../src/poly/overriding/OverridingMain.java" | 24 +++++++++++++++++++ .../src/poly/overriding/Parent.java" | 9 +++++++ 3 files changed, 43 insertions(+) create mode 100644 "\352\271\200\354\225\204\353\246\260/java-basic/src/poly/overriding/Child.java" create mode 100644 "\352\271\200\354\225\204\353\246\260/java-basic/src/poly/overriding/OverridingMain.java" create mode 100644 "\352\271\200\354\225\204\353\246\260/java-basic/src/poly/overriding/Parent.java" diff --git "a/\352\271\200\354\225\204\353\246\260/java-basic/src/poly/overriding/Child.java" "b/\352\271\200\354\225\204\353\246\260/java-basic/src/poly/overriding/Child.java" new file mode 100644 index 0000000..e257897 --- /dev/null +++ "b/\352\271\200\354\225\204\353\246\260/java-basic/src/poly/overriding/Child.java" @@ -0,0 +1,10 @@ +package poly.overriding; + +public class Child extends Parent { + public String value = "child"; + + @Override + public void method() { + System.out.println("Child.method"); + } +} diff --git "a/\352\271\200\354\225\204\353\246\260/java-basic/src/poly/overriding/OverridingMain.java" "b/\352\271\200\354\225\204\353\246\260/java-basic/src/poly/overriding/OverridingMain.java" new file mode 100644 index 0000000..f1117b2 --- /dev/null +++ "b/\352\271\200\354\225\204\353\246\260/java-basic/src/poly/overriding/OverridingMain.java" @@ -0,0 +1,24 @@ +package 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(); + + // 부모 변수가 부모 인스턴스 참조 + Parent parent = new Parent(); + System.out.println("Parent -> Parent"); + System.out.println("value = " + parent.value); + parent.method(); + + // 부모 변수가 자식 인스턴스 참조 (다형적 참조) + Parent poly = new Child(); + System.out.println("Parent -> Child"); + System.out.println("value = " + poly.value); // 변수는 오버라이딩 X + poly.method(); // 메서드는 오버라이딩 + // 오버라이딩 된 메서드는 항상 우선권을 가진다 + } +} diff --git "a/\352\271\200\354\225\204\353\246\260/java-basic/src/poly/overriding/Parent.java" "b/\352\271\200\354\225\204\353\246\260/java-basic/src/poly/overriding/Parent.java" new file mode 100644 index 0000000..82ac175 --- /dev/null +++ "b/\352\271\200\354\225\204\353\246\260/java-basic/src/poly/overriding/Parent.java" @@ -0,0 +1,9 @@ +package poly.overriding; + +public class Parent { + public String value = "parent"; + + public void method() { + System.out.println("Parent.method"); + } +}