File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloaded method question

 
John Park
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
John Park
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12016
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic