From cc5f5909366f86adbbfa0449a0f7a69f4bba9327 Mon Sep 17 00:00:00 2001 From: toychip Date: Tue, 13 Feb 2024 21:57:43 +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(#26)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/week3/poly/overriding/Child.java" | 12 +++++++++ .../poly/overriding/OverridingMain.java" | 25 +++++++++++++++++++ .../java/week3/poly/overriding/Parent.java" | 9 +++++++ 3 files changed, 46 insertions(+) create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/Child.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/OverridingMain.java" create mode 100644 "\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/Parent.java" diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/Child.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/Child.java" new file mode 100644 index 0000000..f92f019 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/Child.java" @@ -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(); + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/OverridingMain.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/OverridingMain.java" new file mode 100644 index 0000000..4a27f2d --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/OverridingMain.java" @@ -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의 메서드 호출, 오버라이딩된 메서드가 항상 우선권을 가진다. + } +} diff --git "a/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/Parent.java" "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/Parent.java" new file mode 100644 index 0000000..238ea94 --- /dev/null +++ "b/\354\236\204\354\244\200\355\230\225/java-basic/src/main/java/week3/poly/overriding/Parent.java" @@ -0,0 +1,9 @@ +package week3.poly.overriding; + +public class Parent { + public String value = "parent"; + + public void method() { + System.out.println("Parent.method"); + } +}