0

I have a class A that has to extend class B and C which are both provided by 3rd party and I don't have a control of them. Since Java doesn't support multiple inheritance. Using interface wouldn't help in my case since I do want to inherit the base classes' implementation and I don't want to copy their code over to my class, or I'll need to be aware of all their coming changes.

What would be the best way to accomplish this need?

All suggestion will be much appreciated!

1
  • why don't use composition instead of inheritance ? having a C and a B in A Commented Jun 21, 2013 at 2:00

1 Answer 1

1
class A

    class B2 extends B

        inherit B's stuff

    class C2 extends C

        inherit C's stuff

    B2 b2 = new B2();
    C2 c2 = new C2();
    // or use anonymous classes

    A has access to everything in B2 and C2,
    therefore A has access to B and C's inheritable assets.
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply! It's a bit similar to what I did. I had Class A extends B and has class C as an inner class, but it doesn't work well since I'll actually need class A is a B and is a C. Is that even possible?
you can have a C asC() method in class A, so if you need to pass a to someone who needs a C, pass a.asC() to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.