wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Innvocation - overridden or overloaded Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Innvocation - overridden or overloaded" Watch "Innvocation - overridden or overloaded" New topic
Author

Innvocation - overridden or overloaded

Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
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.


SCJP 1.4 (93%)<br />SCJD (In progress. It can run, but it can't hide...)
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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.


Mike Gershman
SCJP 1.4, SCWCD in process
Aruna Agrawal
Ranch Hand

Joined: Jan 27, 2005
Posts: 66

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


BOTH
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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 .
 
 
subject: Innvocation - overridden or overloaded
 
Similar Threads
Overloaded question
Doubt -Polymorphism-Overloading vs Overridding as per Kathy Sierra SCJP 1.5 Book?
What is the logic behind designing how Overloaded vs Overridden methods are invoked?
Polymorphism
Polymorphism Teaser