File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Help me to resolve this in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help me to resolve this in Java" Watch "Help me to resolve this in Java" New topic
Author

Help me to resolve this in Java

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

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


When The Going Gets Tougher,The Tougher gets Going
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
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




be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

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

Joined: Jun 14, 2008
Posts: 96
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

Joined: Jul 01, 2008
Posts: 507

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

Joined: Oct 13, 2005
Posts: 36508
    
  16
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

Joined: Oct 13, 2005
Posts: 36508
    
  16
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

Joined: Jun 14, 2008
Posts: 96
yes Deepak, you are right. C doesn't extend B so CImpl does not need to implement methodB.
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Yeah Paul,
Thanks for the reply.Thanks all for your valuable answers and input.
--
Deepak Lal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help me to resolve this in Java
 
Similar Threads
Interface Queries : Java
Java Queries..Please help me...
Collections.synchronizedMap?
POST and REDIRECT IN STRUTS 2...Need Help
Interceptors in Struts 2