Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help me to resolve this in Java

 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have the below query.Please help me to resolve it.


Superclass: Class A.
Class B extends Class A and Class C extends Class A.
Class D extends Class B and Class C,But according to Java Concept you cannot extend more than 1 class.So how i achieve this in Java.?



Example provided will be highly appreciated to solve the above scenario
Help provided will be highly appreciated.


--
Deepak Lal
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Deepak Lal:
Hi,
I have the below query.Please help me to resolve it.




Example provided will be highly appreciated to solve the above scenario
Help provided will be highly appreciated.


--
Deepak Lal


 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

CLASS A -- Correct
CLASS B EXTENDS A -- Correct



CLASS C EXTENDS B
|
|-- The problem lies in this stmt i.e Class C is extending Class B but i want Class C to extend Class A not Class B.I dont want Class C to inherit properties of Class B...Hope its clear now. so how do i achieve it ??


CLASS D EXTENDS C -- this does not come in to picture if the previous step is invalid.

Please reply your comments on this...

--
Deepak Lal
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
classes can't use multiple inheritance but a class can implement multiple interfaces and an interface can extend multiple interfaces.

a possible solution for your problem is to use a combination of interfaces and composition. I've given an indicative solution below. Bear in mind that:
B instanceof A = true
BImpl instanceof A = true
BImpl instanceof AImpl = false

Also, to get this to work correctly, all methods exposed in the concrete implementation of A (AImpl) must be defined in the interface A.



[ November 26, 2008: Message edited by: Paul Beckett ]
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There seems to be a little problem with the below code only.
Can you cross verify if methodB(){} will appear in CImpl class ??

Other parts of the code are perfectly fine.



Thanks for your implementation.

;-)



--
Deepak Lal
 
Campbell Ritchie
Sheriff
Pie
Posts: 50211
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not trying drawing on the screen, but if you draw a UML diagram of your classes A B C D you may see the relationship better. It will look something like this, only with nice squares and proper black heads to the arrows.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50211
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you describe later is called diamond inheritance and looks a bit like thisAnd Paul Beckett has explained how to deal with that. You cannot implement diamond inheritance in Java (nor in C#) by using the extends keyword.
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes Deepak, you are right. C doesn't extend B so CImpl does not need to implement methodB.
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah Paul,
Thanks for the reply.Thanks all for your valuable answers and input.
--
Deepak Lal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic