Java: Alternative to Multiple inheritance

点点圈 提交于 2020-02-07 05:48:04

问题


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:


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.


来源:https://stackoverflow.com/questions/17226364/java-alternative-to-multiple-inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!