• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

Help me to resolve this in Java

 
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 82459
594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 82459
594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah Paul,
Thanks for the reply.Thanks all for your valuable answers and input.
--
Deepak Lal
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic