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.
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.
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 ]
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.
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.