This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt -Polymorphism-Overloading vs Overridding as per Kathy Sierra SCJP 1.5 Book? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt -Polymorphism-Overloading vs Overridding as per Kathy Sierra SCJP 1.5 Book?" Watch "Doubt -Polymorphism-Overloading vs Overridding as per Kathy Sierra SCJP 1.5 Book?" New topic
Author

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

Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
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.


Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt -Polymorphism-Overloading vs Overridding as per Kathy Sierra SCJP 1.5 Book?
 
Similar Threads
What is the logic behind designing how Overloaded vs Overridden methods are invoked?
which version of method is actually called ? the reference one or actual object one ?
Innvocation - overridden or overloaded
Polymorphism Teaser
Polymorphism in Overloaded and Overridden Methods