This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of React Cookbook: Recipes for Mastering the React Framework and have David Griffiths & Dawn Griffiths on-line!
See this thread for details.
Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

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

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Sheriff
Posts: 9686
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
There is no "i" in denial. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic