<p>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)?</p>

<p>Just out of curiosity ;-)</p>

[{'answer_id': 70545, 'author': 'TK.', 'author_id': 1816, 'author_profile': 'https://Stackoverflow.com/users/1816', 'pm_score': 0, 'selected': False, 'text': "<p>JAVA doesn't support multiple Inheritence.</p>\n\n<p>You can get it to implement multiple interfaces and some see this as a way round the problem. Personally I have yet to use multiple inheritence, so I can't really understand its appeal.</p>\n\n<p>Normally when someone suggests multiple inheritence within c# or JAVA its due to the fact that 'they could' in c++. Im a fan of 'just because you can doens't mean you should'. As c# &amp; JAVA doesn't support it, why try and force it to do something it wasn't designed to do. This is not to say that there are unique cases where it is a valid technique to empoly, just the code can usually be refactored to not need it.</p>\n"}, {'answer_id': 70559, 'author': 'Calum', 'author_id': 8434, 'author_profile': 'https://Stackoverflow.com/users/8434', 'pm_score': 2, 'selected': False, 'text': '<p>Use of composition instead of inheritance tends to be the way around this. This actually also helps a lot with testability, so it\'s good practice in general. </p>\n\n<p>If you just want your type to "behave" like several other types, you can inherit from as many interfaces as you like, though; you can\'t "borrow" implementation details from these though, obviously.</p>\n'}, {'answer_id': 70564, 'author': 'Hank Gay', 'author_id': 4203, 'author_profile': 'https://Stackoverflow.com/users/4203', 'pm_score': 1, 'selected': False, 'text': '<p>Use <code>interface</code>s. You can implement as many as you\'d like. You can usually use some variant on the <a href="http://c2.com/cgi/wiki?CompositePattern" rel="nofollow noreferrer">Composite Pattern (GoF)</a> to be able to reuse implementation code if that\'s desirable.</p>\n'}, {'answer_id': 70565, 'author': 'Steve Morgan', 'author_id': 5806, 'author_profile': 'https://Stackoverflow.com/users/5806', 'pm_score': 2, 'selected': False, 'text': '<p>I believe that the fundamental reason that Java doesn\'t support multiple inheritance is the same as C#; all objects are ultimately derived from Object and it\'s having multiple paths to the same base class is ambiguous for the compiler. Ambiguous == Bad, so the compiler doesn\'t allow it.</p>\n\n<p>Instead, you can simulate multiple inheritance through delegation. See <a href="http://www.javaworld.com/javaworld/jw-10-2005/jw-1024-multiple.html" rel="nofollow noreferrer">this article</a> for an example.</p>\n'}, {'answer_id': 70606, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>You can cheat it a little (and I stress a little) by using java.lang.reflect.Proxy instances. </p>\n\n<p>This really just allows you to add extra interfaces and delegate their calls to another instance at runtime.</p>\n\n<p>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).</p>\n"}, {'answer_id': 70623, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>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). </p>\n\n<p>Java provides interface inheritance by the <em>implements</em> mechanism and you <em>can</em> have multiple interface inheritance.</p>\n\n<p>Implementation inheritance is the <em>extends</em> mechanism and you've only got a single version of that. Do you <em>really</em> need multiple implementation inheritance? I bet you don't, it's chock full of unpleasant consequences, unless you're an Eiffel programmer anyway. </p>\n"}, {'answer_id': 70635, 'author': 'Bert F', 'author_id': 11296, 'author_profile': 'https://Stackoverflow.com/users/11296', 'pm_score': 1, 'selected': False, 'text': '<p>You could probably "simulate" it by managing the set of superclasses explicitly and using reflection to search all the superclasses for the target method.  I wouldn\'t want to do this in production, but it might an interesting toy program.  You could probably do a lot of weird stuff by leveraging reflection, creating classes on the fly, and invoking the compiler programatically.</p>\n'}, {'answer_id': 70764, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>I was thinking about this a little more and realised that while dynamic proxies will work (it\'s how RMI (used?) to work), if you really want this sort of functionality you would be better off looking at aspect oriented programming (AOP) using something like AspectJ (eclipse.org/aspectj).</p>\n\n<p>This way you can get several different aspects into a class, giving you pseudo mixin inheritance, without the hideously fragile inheritance heirarchies.</p>\n\n<p>As everyone else has pointed out, wanting/needing multiple inheritance generally indicates you aren\'t approaching the problem from the right perspective. Remember the GoF principle of "prefer composition over inheritance" for a start!</p>\n'}, {'answer_id': 70996, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>There was an effort to bring mixins into Java. Check this link out: <a href="http://www.disi.unige.it/person/LagorioG/jam/" rel="nofollow noreferrer">http://www.disi.unige.it/person/LagorioG/jam/</a></p>\n'}, {'answer_id': 71087, 'author': 'Michael Bar-Sinai', 'author_id': 11699, 'author_profile': 'https://Stackoverflow.com/users/11699', 'pm_score': 5, 'selected': True, 'text': '<p>Sure you can, but it\'s tricky and you should really consider if that\'s the way you want to go.<br>\nThe 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.<br>\nGilad Bracha (one of the main java language designers) wrote a <a href="http://bracha.org/dyla.pdf" rel="nofollow noreferrer">paper</a> discussing that.\nSo, 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.  </p>\n\n<p>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 ;-)</p>\n'}, {'answer_id': 327260, 'author': 'Warrior', 'author_id': 40933, 'author_profile': 'https://Stackoverflow.com/users/40933', 'pm_score': 3, 'selected': False, 'text': '<p><strike>Single</strike>Multiple 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++. </p>\n'}, {'answer_id': 854723, 'author': 'Özgür', 'author_id': 12652, 'author_profile': 'https://Stackoverflow.com/users/12652', 'pm_score': 0, 'selected': False, 'text': '<p>By using Inner Classes, this is what C++ sometimes prefers as well: <a href="http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Inner_Class" rel="nofollow noreferrer">Inner Class Idiom</a>.</p>\n'}, {'answer_id': 46753756, 'author': 'Abdur Rehman Khalid', 'author_id': 8066227, 'author_profile': 'https://Stackoverflow.com/users/8066227', 'pm_score': -1, 'selected': False, 'text': "<p>Yes you can say that it's a trick and it is very Interesting you cannot inherit multiple classes to a single class but it is possible to implement multiple Interfaces to a class like</p>\n\n<pre><code>public class parents implements first, second{\n\n}\n</code></pre>\n\n<p>but remember, you have to override methods declared in interfaces.</p>\n"}]
['https://Stackoverflow.com/questions/70537', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11193/']