i know that overloaded methods are determined at compile time based on the reference type which invokes the method while overrided methods is determined by the actual object type of the reference invoking he method, the question is why polymorphism only matters about overriding and not overloading?
i know that overloaded methods are determined at compile time based on the reference
Share
The implementation mechanism that makes polymorphism in Java possible is the dynamic dispatch of a method based on the runtime type of its first argument (in a call
a.method(b, c),acan be considered the first argument of the method). Java is therefore a single dispatch OOP language. The upshot is that all other arguments don’t participate in this mechanism and their type is determined statically at compile time. So for example if you haveand then
can never print
truesince your method is not being called. The compiler saw a callMyObject.equals(Object)and compiled it as a call of the inherited methodObject.equals(Object). The runtime method dispatch mechanism will only decide which overriding methodequals(Object)to call.