File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Overloaded method question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overloaded method question" Watch "Overloaded method question" New topic
Author

Overloaded method question

John Park
Greenhorn

Joined: Aug 04, 2005
Posts: 27
Hi all,

I understand that when you instantiate sub-class object type B to a super class reference type A, and assuming that there are 2 overloaded methods, one passing in the superclass type, the other the subclass type, that the compiler will always look for the overloaded method passing in the reference type.

My question is that why would anyone declare A a = new B(); in the first place , instead of A a new A();?

Why would you set a super class reference to a subclass type?
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I hate to answer a question with a question. But why would you overload a method to do something differently for the subclass than its parent.
A situation like that would alert me that the responsibilities are misplaced and doIt() should be a method in A which is overridden by B. (And which would possibly take a Foo as a parameter.


[ August 29, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
John Park
Greenhorn

Joined: Aug 04, 2005
Posts: 27
Thanks for the response Garrett,

So with the 1st Code example you gave, if you did this:
class Foo{

//overloaded methods...

public static void main (String [] args)
{
A apple = new B();
a.doIt(apple);

Since both methods are overloaded the result would be apple invoking the method passing in the super-type A.

But why set apple to a new B object? I don't see any point in that..
Bridget Kennedy
Ranch Hand

Joined: Nov 30, 2004
Posts: 84
I can only think of one scenario in which you would want to initialized a local variable in this fashion: Somewhere else in the method, you need to reassign 'a' to some other type of class - either A, or something other than B that extends A. If you only have a need for a reference to class B, it makes sense to simply code: B b = new B(); Or, if you are only using methods from A, A a = new A();

Your question really gets to the crux of the purpose of polymorphism, and it can be a tricky concept to grasp. Luckily, there are tons of words out there to help you understand how it all fits into software design.

Good luck!
[ August 29, 2006: Message edited by: Bridget Kennedy ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Actually the first code would be invoked like:


As for why you yould say:
A apple = new B();

if you extend a class you usually provide it with some functionality that isn't available in the base class.

So now every method that takes an Engine as a parameter can also take a SuperChargedNASCAREngine. The method doesn't know at run time what type of Engine you have, and it doesn't care. As long as it receives an Engine who's methods it can invoke, it is happy. However suppose next week you find that a SuperChargedNASCAREngine isn't really what you need (fuel is expensive these days), you can easily change it to a


The point is once you have the Engine, it doesn't matter to the rest of the code what type of Engine it is, all that truly matters is that it is an Engine

[ August 29, 2006: Message edited by: Garrett Rowe ]
[ August 29, 2006: Message edited by: Garrett Rowe ]
John Park
Greenhorn

Joined: Aug 04, 2005
Posts: 27
sorry to sound confusing to all..if it helps any, i'm referring to pages 307 - 309 in the Java 2 Sun Certified Programmer & Developer Study Guide, copyright 2003 by Sierra/Bates.

The section on invoking overloaded methods. If you have 2 classes, Animal, and Horse, Horse extends Animal...

and when you use an Animal reference to a Horse object:

Animal animalRefToHorse = new Horse();

the animalRefToHorse reference passed into an overloaded method will always return the method which passes in type..

I don't know why you would do this:

Animal a = new Horse();

if you already know that the overloaded method you're going to invoke is going to return the job from the method passing in type Animal..

Why can't you just do Animal a = new Animal();

Thanks,

JP
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Ahhhh... I didn't realize that your question was referring to an exam prep type question. In that case you can throw out my first response because it deosnt apply. The question then becomes what does the overloaded method do with the Animal you pass it. If it invokes an instance method of the Animal then the question becomes do you want it to invoke the Animal version or the Horse (overridden) version.
John Park
Greenhorn

Joined: Aug 04, 2005
Posts: 27
Sorry, I should have stated that this was prep for exam..basically this section was stressing that anytime you have this:

class Animal{}
class Horse extends Animal{}
class TestAnimals{

public void doIt(Animal a){}
public void doIt(Horse b){}

public static void main(String [] args){

TestAnimals ta = new TestAnimals();

Animal a = new Horse(); //why set it to a horse object, and why not //an Animal object?


a.doIt(ta); //this will invoke void doIt(Animal a);

Sorry to be such a pain in the ass...

Thanks,

JP
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Like I stated in my previous post, it depends on what the doIt() method actually does with the Animal instance passed to it.




Output:
In doIt(Animal)
Eating hay... because I'm a Horse
In doIt(Animal)
Eating... because all animals eat
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11313
    
  16

Animal a = new Horse(); //why set it to a horse object, and why not //an Animal object?


because you might want a Horse object.

"so why don't you use a Horse reference?", i hear you ask...

because later in my code, i may want a Donkey, or a Turkey, or a YellowBelliedSapSucker. but i want to use the SAME reference variable.

this is often used when you are pulling objects out of a collection. i create an Array (or other collection) of Animals. i can stick in ANYTHING that is an animal.

if i then want to call the eat() method on all of them, the easiest way is to iterate through the list, and use an Animal reference for each. i don't want to have to do soemthing like this:



when i could just do this:

[CODE]for (int i = 0; i < myArray.length; i++){
Animal temp = myArray[i];
Animal.eat();
}
{/code]

i can create new types of Animals at ANY point, stick them in, and the second code will work without being touched. the first code sample you'd have to add a new else if statement for every new type of Animal.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloaded method question