Cheat single inheritance in Java?

2019-02-05 01:43发布

I have heard there is a way to cheat single inheritance and implement multiple inheritance in Java. Does anyone know how to implement this(with out using interface)?

Just out of curiosity ;-)

13条回答
家丑人穷心不美
2楼-- · 2019-02-05 02:22

Sure you can, but it's tricky and you should really consider if that's the way you want to go.
The idea is to use scope-based inheritance coupled with type-based one. Which is type-talk for saying that for internal purposes, inner classes "inherit" methods and fields of the outer class. It's a bit like mixins, where the outer class is mixed-in to the inner class, but not as safe, as you can change the state of the outer class as well as use its methods.
Gilad Bracha (one of the main java language designers) wrote a paper discussing that. So, suppose you want to share some methods for internal use between some unrelated classes (e.g, for string manipulation), you can create sub classes of them as inner classes of a class that has all the needed methods, and the sub classes could use methods both from their super classes and from the outer class.

Anyway, it's tricky for complex classes, and you could get most of the functionality using static imports (from java 5 on). Great question for job interviews and pub quizzes, though ;-)

查看更多
神经病院院长
3楼-- · 2019-02-05 02:24

You need to be careful to distinguish interface inheritance (essentially inheritance of a contract to provide particular facilities) from implementation inheritance (inheritance of implementation mechanisms).

Java provides interface inheritance by the implements mechanism and you can have multiple interface inheritance.

Implementation inheritance is the extends mechanism and you've only got a single version of that. Do you really need multiple implementation inheritance? I bet you don't, it's chock full of unpleasant consequences, unless you're an Eiffel programmer anyway.

查看更多
姐就是有狂的资本
4楼-- · 2019-02-05 02:31

By using Inner Classes, this is what C++ sometimes prefers as well: Inner Class Idiom.

查看更多
▲ chillily
5楼-- · 2019-02-05 02:32

You can cheat it a little (and I stress a little) by using java.lang.reflect.Proxy instances.

This really just allows you to add extra interfaces and delegate their calls to another instance at runtime.

As someone who mentors and tutors new developers I would be horrified if somebody showed me code that did this. Reflection is one of those tools that you really need to understand and have a good understanding of Java before jumping in. I personally have only ever done this once, and it was to make some code I didn't have control over implement some interfaces some other code I had no control over was expecting (it was a quick hack so I didn't have to write and maintain too much glue code).

查看更多
三岁会撩人
6楼-- · 2019-02-05 02:32

Use interfaces. You can implement as many as you'd like. You can usually use some variant on the Composite Pattern (GoF) to be able to reuse implementation code if that's desirable.

查看更多
来,给爷笑一个
7楼-- · 2019-02-05 02:35

SingleMultiple inheritance is not supported by Java, instead it has got interfaces to serve the same purpose. In case you are adamant on using multiple inheritance it should be done in C++.

查看更多
登录 后发表回答