인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

alsrhks21542156's profile image
alsrhks21542156

asked

Kim Young-han's Practical Java - Fundamentals

Inheritance and Method Overriding

인스턴스 타입을 부모로해도 자식의 오버라이드 메서드가 호출되나요?

Resolved

Written on

·

493

·

Edited

0

안녕하세요!

 

ElectricCar electricCar = new ElectricCar();
electricCar.move();

이렇게 하면 호출한 electricCar의 타입은 ElectiricCar라서 인스턴스 내부의 ElectricCar 타입에서 시작한다고 하셨습니다.

그래서 electricCar.move();를 실행하면 ElectricCarmove() 메서드가 실행돼서

전기차를 빠르게 이동합니다.

이렇게 전기차로 출력이 됩니다.

Car electricCar = new ElectricCar();
electricCar.move();

그럼 이렇게 타입이 Car타입이면
인스턴스 내부의 Car타입에서 move()메서드를 실행시켜 "차를 이동합니다."가 출력될 것이라고 생각했는데

전기차를 빠르게 이동합니다.

여기서도 전기차를 빠르게 이동합니다.가 출력됩니다.


어째서 Car타입에서 move()메서드를 실행하지 않고 ElectricCarmove()메서드를 실행하게 되는건가요?

감사합니다!!😊

java객체지향상속오버라이딩

Answer 1

2

yh님의 프로필 이미지
yh
Instructor

안녕하세요. 노뚜님!

조금 뒤에 나오는 다형성을 학습하시면 바로 이해하실 수 있을거에요 🙂

감사합니다.

alsrhks21542156님의 프로필 이미지
alsrhks21542156
Questioner

그렇군요!!ㅜ 감사합니다😁

alsrhks21542156's profile image
alsrhks21542156

asked

Ask a question