aspose file tools*
The moose likes Beginning Java and the fly likes Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Polymorphism" Watch "Polymorphism" New topic
Author

Polymorphism

Shelly Biswal
Greenhorn

Joined: Sep 07, 2007
Posts: 17
Hi All,

My question is how to use polymorphism to solve the problem described.

I have two Frameworkd Fr1 and Fr2.

Fr1 has a class with method abc(A a) which takes an instance of some class A
Fr2 has a class with method xyz(B b) which takes an instance of some class B

I have a Class where i am instantiating an instance of Some Class X.
X x = new X();
now i want to pass the above instance x to both the methods abc() and xyz() in two different framewroks.
how would i do that using polymorphism?
I understand that i can make Class X extend from A or B and pass x instance but we cant extend from more than one class.
So how do i solve the above?
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Hi,
The only possibility i can see in your problem is that..

you want to pass x instance to both abc(A a) and xyz(B b), Just tell me How two different reference type (a and b) can accept X Object..The only one possible way is they must have to be in same hirerachy...!! so as per my knowledge the only solution is let B inherits A..and X inherits B..!!!


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Shelly Biswal
Greenhorn

Joined: Sep 07, 2007
Posts: 17
The only possibility i can see in your problem is that..

you want to pass x instance to both abc(A a) and xyz(B b), Just tell me How two different reference type (a and b) can accept X Object..The only one possible way is they must have to be in same hirerachy...!! so as per my knowledge the only solution is let B inherits A..and X inherits B..!!!


The problem above is my methods are in different frameworks, as in, not in the same heirarchy.They are two different classes from any location but they are not aware of each other.
If B inherits from A then B compulsorily will have all the methods from A which is not desired here.
Mark Newton
Ranch Hand

Joined: Jan 31, 2006
Posts: 129
How much control to you have over your two frameworks? Could both A and B implement the same interface?

For example:

Then, you would need to change your two methods to:

Any good?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

In that case, they will have to have something in common.

I guess what David Payne suggested sounds valid!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Shelly Biswal
Greenhorn

Joined: Sep 07, 2007
Posts: 17
Thanks guys n thanks to david.
my problem is solved.
i guess thats what interfaces are for !
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

That's Great Shelly!

Congrats! You are right!
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

If you have that much flexibility of defining interface in those framework and changing the argument of both the methods,then you could have done
B inherits A
C inherits B
this also..!!!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
A different solution would have been the Adapter design pattern: http://en.wikipedia.org/wiki/Adapter_pattern


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphism