• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism

 
Shelly Biswal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..!!!
 
Shelly Biswal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, they will have to have something in common.

I guess what David Payne suggested sounds valid!
 
Shelly Biswal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys n thanks to david.
my problem is solved.
i guess thats what interfaces are for !
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's Great Shelly!

Congrats! You are right!
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A different solution would have been the Adapter design pattern: http://en.wikipedia.org/wiki/Adapter_pattern
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic