45. 자바 ( 상속2 )

jhnada·2022년 8월 3일
0

상속 - 재설정

class Animal {
	int num = 10;
}
class Dog extends Animal {
	int num = 20;
}
public class Extends02 {

	public static void main(String[] args) {

		Dog d1 = new Dog(); 
        // Dog, num 20 과 -> Animal num 10 뜸
		System.out.println(d1.num);
	}

}

이때 Dog d1 = new Dog();
는 heap에 Animal과 Dog 두개의 공간을 띄우고
가리키는곳은 Dog 이기 때문에 num은 출력은 ' 20 ' 이나온다.
Animal d1 = new Dog();
로 할시에는 num은 10이기 때문에 출력은 ' 10 ' 이나온다.

profile
밑바닥부터 배우는 초짜 개발자

0개의 댓글