aspose file tools*
The moose likes Beginning Java and the fly likes Polymorphism against overload methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Polymorphism against overload methods" Watch "Polymorphism against overload methods" New topic
Author

Polymorphism against overload methods

giovanni giuseppe
Greenhorn

Joined: Feb 16, 2010
Posts: 6
Hi,
first sorry for my english...

I have a problem. I have two classes : father class (Father) and a subclass (Son) that inherits from Father.
In another class i declare :

int flag;
Father father = getRightClass();

//further code here

public Father getRightClass(){flag == 0 ? new Father(): new Son();}

Now How can i create two overloaded methods (es. method(Father) and method(Son) ) to get correctly object, if it is the refernce type that discriminates what method will be called?

And declare an object with an instance type that is a subclass of reference type is polimorphism?
Thanks.
Suhas Bilaye
Ranch Hand

Joined: Sep 10, 2009
Posts: 80
Hi,

Welcome to JavaRanch!!!

Overloading
As you have rightly said, it is the reference type that decides which overloaded method will be called(if the arguments fall in the same class hierarchy else the type and no of arguments decide which overloaded method is called).

Overriding
If you want to use the Superclass reference for a subclass object and want to call the right method depending on the object and not on the reference then you can use overriding which uses runtime polymorphism i.e it decides which class method should be called depending on the object rather than the reference type.




Output will be




Thanks and Regards,
Suhas
http://www.xplore-java.blogspot.com/
giovanni giuseppe
Greenhorn

Joined: Feb 16, 2010
Posts: 6
thanks a lot...

but the problem is that i have to declare the methods outside the class Father and Son...

There is another solution or the best way is only to declare the methods within the classes and using overriden methods?
Hetal Vora
Greenhorn

Joined: Feb 11, 2009
Posts: 12
Just to give you an example of method overloading, I am just roughly outlining your classes as below:

class Father{
void method1(Father f){}
Father static getRightObject(){if(some flag) return new Father(); else return new Son();}
}
class Son extends Father{
void method1(Son s){}
}

Father f = Father.getRightObject();
If getRightObject() returns you a father class object, the invocation as f.method1(f) will ofcourse invoke Father class method1.

If getRightObject() returns you a Son object, the invocation f.method1(f) still invokes Father class method1 since the reference f used for invocation is a Father reference and can see only its method1. So, even if you were to invocation syntax is f.method1((Son)f), it still invokes Father class method1.

If you are sure that the object returned is Son object, you can do the following to invoke Son class method1()
((Son)f).method1((Son)f);


Thanks and Regards, Hetal
SCJP 1.5 100%
Suhas Bilaye
Ranch Hand

Joined: Sep 10, 2009
Posts: 80
but the problem is that i have to declare the methods outside the class Father and Son


Well overloading is compile time polymorphism and overriding is run time polymorphism, the requirement that you have is you want to use Father reference to point to a Son or Father object and the call to method() should call the respective method during runtime depending on the object and not on the reference.
Since you are actually expecting runtime polymorphism in your scenario overriding is the only option.

Regards,
Suhas
giovanni giuseppe
Greenhorn

Joined: Feb 16, 2010
Posts: 6
Yes ithink that is the only solution.....thanks at all!!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Polymorphism against overload methods