• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Innvocation - overridden or overloaded

 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

RE: The "Java 2" book by Kathy Sierra & Bert Bates, Chapter 5, page 311.

Given...

public class Horse extends Animal {
eat(){}
eat(String){}
}
public classAnimal {
eat(){}
}

Table 5-3 explains that if a method is overloaded then the "Reference Type" of the object determines which overloaded version is run. Whereas if the method is overridden then the "Object Type" determines which method is selected.

Logically, if an object invokes eat(), since the method is overridden, then the object type at run time determines whether to invoke the Animal.eat() or the Horse.eat(). Contrariwise, if an object invokes eat(String), since the method is overloaded the reference type at compile time determines the method signature.

BUT...suppose...Animal also has eat(String){}

public class Horse extends Animal {
eat(){}
eat(String){}
}
public classAnimal {
eat(){}
eat(String) {} //This has been added...
}

Now, is eat(string) overridden or overloaded or both? How do I calculate whether Horse.eat(String) or Animal.eat(String) is run?

Cheers,

Simon.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that case, Java chooses the method signature at compile time based on the overloading rules and chooses the specific method of that signature at run time using the overriding rules.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


is eat(string) overridden or overloaded or both? How do I calculate whether Horse.eat(String) or Animal.eat(String) is run?



BOTH
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes , it is overloding & overriding both . But the method will be called beased on object type ( At run time ) .

Aminal aOb = new Horse();
aOb.eat("Grass");

In this case , eat(String) method of Horse class will be called .
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic