Grokking Java Interview Questions
Vote

0% completed

​

Polymorphism and Dynamic Dispatch

A variable has the declared type PriceService, and it refers to an object of the subclass MemberPriceService. Both classes declare a field cents and a method price(). If you read the field and call the method through this variable, do both come from the same class?

They do not. The method comes from the subclass, and the field comes from the parent. Four questions lead to the reasons: what polymorphism is, how the JVM selects a method, what happens with fields and static methods, and how casts change the declared type but not the object.

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content