3

I very well know that it can be done with the help of interfaces and i have done it many times. But this time my situation is quite difference. I have class A , class B and i need to create another class C which extends both A and B because C should have boths functionality and also note that A and B are not inter related so even i cant say A may extend class B.

I am quite confused what should i do right now. I know we cant change java... but at least there would be some way possible. Even the nearest may also do... please help me out.

Adding more details:- Class B is a standard API while class A is a common exception class that need to be inherited by all exception classes.

Related question:

Community
  • 1
  • 1
amod
  • 4,190
  • 10
  • 49
  • 75
  • 1
    It would help if we knew if you have control over `class A` and `class B`. – Eric Wilson Aug 29 '11 at 13:22
  • possible duplicate of [Why there is no multiple inheritance in Java, but implementing multiple interfaces is allowed](http://stackoverflow.com/questions/2515477/why-there-is-no-multiple-inheritance-in-java-but-implementing-multiple-interface) – Riduidel Aug 29 '11 at 13:26
  • 1
    @ riduidel this is not possible duplicate... please dont close this question. – amod Aug 29 '11 at 13:27
  • @ eric i may have some control on A... actually A is a exception class and all exception class must inherit it. Also note that B is an standard API so no control on it. – amod Aug 29 '11 at 13:37
  • You don't use multiple inheritance to inherit from an Exception and from an standard API class, sorry that is simply wrong and makes no sense at all. – Angel O'Sphere Aug 29 '11 at 14:52
  • actually that standard Api is also a type of exception that may occur in my code. So now i guess it make sense... class A is a exception class which represents a kind of exception i need to inherit it( as per the design pattern) and class B is a exception which is definetly required. – amod Aug 29 '11 at 14:58

8 Answers8

11

This is typically solved using object composition.

This is also advocated in Effective Java, Item 16: Favor composition over inheritance.

Java restricts a class from having an is a-relation to two (unrelated) classes. It does not however restrict a class from having the functionality of two unrelated classes.


class A {
    void doAStuff() {
        System.out.println("A-method");
    }
}

class B {
    void doBStuff() {
        System.out.println("B-method");
    }
}

// Wraps an A and a B object.
class C {
    A aObj;
    B bObj;

    void doAStuff() {
        aObj.doAStuff();
    }

    void doBStuff() {
        bObj.doBStuff();
    }
}

(Alternatively you could have class C extends A and only create wrapper methods for B, but keep in mind that it should make sense to say C is an A.)


I have a design pattern that need to be followed and for that its compulsory to extend

This is, as you probably know completely impossible. You could however create proxy classes for A and B that delegate the functionality to the C-instance. For instance by adding these two methods to the C-class above:

class C {

    // ...

    public A asAObject() {
        return new A() {
            @Override
            void doAStuff() {
                C.this.doAStuff();
            }
        };
    }

    public B asBObject() {
        return new B() {
            @Override
            void doBStuff() {
                C.this.doBStuff();
            }
        };
    }
}
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 1
    additionally, you could extract the interfaces of A and B and make C implement them - if you change the code where A and B are used to use the interfaces instead, you can just stick C in there ;) – Rommudoh Aug 29 '11 at 14:13
  • 1
    yup... if he has control over the relevant classes, that's a really good approach. – aioobe Aug 29 '11 at 14:44
  • 2
    _Favor composition over inheritance_ One of my favourits :-) – Wivani Aug 29 '11 at 14:52
5

http://en.wikipedia.org/wiki/Adapter_pattern

Your problem is solved by the adapter pattern. I went back to check the wiki document, just to be sure :)

A myth
  • 86
  • 1
  • 4
3

It is not possible to extend two classes, and thus inherit functionality from both, in Java. Your easiest alternative it to "wrap" at least one of the two classes. So, instead of also extending B you can have an instance of B inside C, and declare each of B's methods on C, and pass them through. C would not "be" a B, but it sounds like you just want to inherit functionality. And if that's true of A too, then, well, you should be using this pattern for both A and B and not extending either.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
  • thanks for the reply but i dont want only functionality i have a design pattern that need to be followed and for that its compulsory to extend. – amod Aug 29 '11 at 13:29
  • 1
    commented on this in my answer. – aioobe Aug 29 '11 at 13:35
  • You can not "have a design pattern" that "needs to be followed". That makes no sense! You should try to tell us what you want (want to accomplish) and we can help you solve your problem then. – Angel O'Sphere Aug 29 '11 at 14:50
1

Make use of composition. Class C will contain an instance each of classes A and B.

adarshr
  • 61,315
  • 23
  • 138
  • 167
1

You may try create class C that will contain one instance of class A and one of class B and their methods (create methods with same names that will just call methods from A and B), it's called Composition - another method in opposite to inheritance. Of course that's in case if you don't need instanceof to work with that C class.

nidu
  • 549
  • 2
  • 18
1

A common oop principle is "choose composition over inheritance", since you can't extend 2 classes I would suggest having one of or both of those classes as members of Class C, that is of course if that works for what you are trying to do.

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
1

Maybe you can create a new class D, and have both class A and class B inherit from D?

Rivka
  • 823
  • 2
  • 7
  • 20
1

Change the way you are thinking. Think in interfaces. It means that class A is actually an implementation of interface A' and class B is an implementation of interface B'. And you actually want a class C that implements both interfaces A' and B'. Class C doesn't need to extend anything or to use any particular pattern or whatsoever. It can extend A and delegate some methods to B, or it can extend B and delegate some methods to A. Or it can delegate all methods to A and B - implementation is your decision.

Infeligo
  • 11,715
  • 8
  • 38
  • 50
  • Does not work for exceptions ... he wants a base for Exceptions, but I gues he actually does not really knwo what he wants. – Angel O'Sphere Aug 29 '11 at 14:51