Forums Register Login

Doubt -Polymorphism-Overloading vs Overridding as per Kathy Sierra SCJP 1.5 Book?

+Pie Number of slices to send: Send
Hi All,

I read many different materials on Overloading and Overridding difference.But i am kind of confused.Please help me.


I understand that, Overloading :Static /Compile Time polymorphism and OverRidding :Run time Polymorphism


1.Chapter 2 :pg 109 :It says Polymorphism doesnt deterimine which overloaded version is called (but in overridden version )

2.In.N differences between Overloading and Overridding in the book(chapter 2 ,page 111)
Last point is on invocation ,where major confusion is.

For overridding,method call is dependent on Reference type but what method -defintion is executed ,is decided at Run time.
But in book ,its says -
For Overloading, Reference type-determines which overloaded version to call.
Isnt it same fo OverRidding ,Reference type-determines which method can be called.

eg
Class Animal {
void eat(){} //1
}
Class Horse extends Animal{

void eat(){} //2 -Overridding
void run(){} //3 //new mthod

}


now ,Animal ah=new Horse();
ah.eat(); //works and calls 2 ,Horse's version of eat()

ah.run()// Compile time failure-since Animal doesnt have run()

So in Overridding also ,its determined at Compile time ,although method version is determined at Runtime.


Please clear me of this confusion or suggest me some material i can go through.


+Pie Number of slices to send: Send
 

Lucky J Verma wrote:So in Overridding also ,its determined at Compile time ,although method version is determined at Runtime.



The example that you gave with you not being able to call run() method on Animal object is right. You cannot call run method on an Animal reference. But how are you associating it with Overriding. There is no overriding involved there. The run method in Horse class, doesn't override any method.
(The only compile time check that you can associate with overriding is that you should have access to the overridden method to call it, and the actual method call will be resolved at run time)

But in book ,its says -
For Overloading, Reference type-determines which overloaded version to call.



Are you talking about this



If yes, then this is a completely different thing. How are you confusing Overloading and Overriding, they are very different things. Definitely here the reference type will decide which method will be called. I'm still not sure what you are confused at...
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1394 times.
Similar Threads
Innvocation - overridden or overloaded
Polymorphism Teaser
What is the logic behind designing how Overloaded vs Overridden methods are invoked?
Polymorphism in Overloaded and Overridden Methods
which version of method is actually called ? the reference one or actual object one ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 10:07:19.