package com.java1.day16;
/**
* 조상 클래스에 선언된 멤버변수와 같은 이름의 인스턴스 변수를 자손 클래스에 중복으로 정의 했을때,메서드의 경우 항상 실제 인스턴스의
* 메서드(오버라이딩된 메서드)가 호출 되지만, 맴버변수의 경우 참조 변수의 타입에 따라 달라진다.
*
* @author user
*/
public class BindingTest {
public static void main(String[] args) {
Parent p = new Child(); // 자식 -> 부모
Child c = new Child();
System.out.println("p.x = " + p.x);
p.method();
System.out.println("c.x = " + c.x);
c.method();
}
}
class Parent {
int x = 100; // 자손 클래스 Child에도 int x = 200; 변수가 선언되어 있다.
void method() {
System.out.println("Parent Method");
}
}
class Child extends Parent {
int x = 200;
@Override
void method() {
System.out.println("Child Method");
}
}
p.x = 100
Child Method
c.x = 200
Child Method