Why is multiple inheritance bad?

Hi all,
Can someone give me examples explaining why multiple
inheritence is bad?
My arguments, why the absence multiple inheritence
is bad are below:
1) The absence of multiple inheritance gives me sometimes
headaches. If I implement an interface for overriding just
one method, I must then in some cases (likely in
AWT-programming) "empty-implement" 10 other methods.
2) If I use the adapter class to avoid this, then I cannot
derive my class from another one.

In my opinion, the lack of multiple inheritance is
not a real problem. You can easily bypass this using
a delegate pattern. Example:
class Base1 {
public void method1() {
class Base2 {
public void method2() {
If you want to create a class which inherits from
Base1 and Base2, take the following approach:
Create Interfaces for Base1 and Base2:
intface BaseInterface1 {
public void method1();
interface BaseInterface22 {
public void method2();
Now, let Base1 and Base2 implement the Interfaces.
class Base1 implements Base1Interface {
public void method1() {
class Base2 implements Base2Interface {
public void method2() {
The the subclass can inherit from one baseclass
and implement the other one. Base2 is an attribute
(instance variable) of the new class:
class Devided extends Base1 implements Base2Interface {
Base2 base2;
// Methods of Base2Interface:
public void method2() {
base2.method2();
This looks like a lot of handcraft, but modern IDEs
like Eclipse or VisualAge (ok, not really modern)
give the developer the advantages of self-written
plug-ins. Using such a plug-in, you can easily
automate the creation of the interfaces and the
delegate. In my previous company we wrote several
"wizards" for VisualAge to prevent us from doing
stupid steps again and again.

Similar Messages

  • Why java does not support multiple inheritance ???

    Hai friends ..iam new to java .. i have doubt ..plz help me
    Why java does not support multiple inheritance ???

    The reasons for omitting multiple inheritance from the Java language mostly stem from the "simple, object oriented, and familiar" goal.
    To understand multiple inheritance, the learner needs some level of expertise like virtual derivations etc in c++. Multiple inheritance will allow method duplication, and throws the learner into confusion which method might be called by the compiler in which scenario at run time.
    Even though this answer seems to be funny, this is the actual reason why java omitted multiple inheritance of classes.
    But java support multiple inheritance of interfaces. Multiple interface inheritance allows an object to inherit many different method signatures with the caveat that the inheriting object must implement those inherited methods.

  • Multiple inheritance, delegation and syntactic sugar

    Given that delegation of an interface to a class member gives you most of the effect of multiple inheritance, why isn't there any syntactic sugar in the language to make this easy?
    For example, you could have:
    class B extends A implements I delegate i { I i = new IAdapter(); }
    The semantics is simple: for each method in each delegated interface, if there is no method with that signature defined in the class then create one with a body that delegates the call to the given member. If there is an ambiguity from multiple interfaces, just flag it at compile time so that the programmer must add an explicit method.
    This doesn't impact the class file format as it isn't doing anything that can't be done longhand in the code. It would even provide some protection from interface changes, as a recompile would pass the problem on to the delegate (which would likely inherit from some standard adapter class which would be modified at the same time as the interface).
    Why isn't this done (apart from because MI is inherently evil and just suggesting this addition means I'm a bad person). It would make the language a tiny bit bigger, but at least when people ask why Java doesn't have proper MI you could answer 'it does and you do it like this' with an almost straight face.
    Jonty

    The only problem with this is that with multiple
    delegates it kinda falls apart.
    When yourFunc() is called on an instance of A, which
    function is called?original> If there is an ambiguity from multiple interfaces, just flag it at compile
    original> time so that the programmer must add an explicit method.
    My suggestion was that if there is any ambiguity then no delegation is made, forcing the programmer to do it manually.
    Normally in Java it's no big deal when multiple
    interfaces have the same methods in them.Actually, I find it a bit of an odd choice by the language designers allowing same signature functions defined in different interfaces to not generate a name clash. Normally this indicates that there is, or soon will be, a bug. I can't think of any situation where this is good programming - if you want the method to exist in both interfaces then it should be defined in a third and inherited into both. This makes it clear that it really is the same method.
    Not saying that delegates are a bad idea, but simple
    examples don't prove that delegates are a worthwhile
    feature to add.The point is that delegation is a commonly used pattern, and is almost always trotted out as a good way of getting most of multiple inheritance. The comments so far are about the problems with this pattern, not with my suggestion to make it easier to use.

  • Alternative for multiple inheritance (AbstractQueue and AbstractList)

    Hello all,
    What is the best alternative for multiple inheritance? I want to make a list, which supports queue operations. Both AbstractQueue en AbstractList are useful, so I would like to use them both, instead of implementing the methods myself or copying the code.
    thanks

    Do you mean you want a class just like LinkedList?
    Why don't you look at the code for LinkedList. Perhaps you could even use it.
    Most Queue methods have trivial implmentations using a List.
    From the source for LinkedList
    public class LinkedList<E>
        extends AbstractSequentialList<E>
        implements List<E>, Queue<E>, Cloneable, java.io.Serializable

  • Question about multiple inheritance

    Why does java not support multiple inheritance, but also give you the ability to use interfaces?
    I've done a quick search on here which turned up the same thing as the books on java I've read - they tell me that java doesn't support multiple inheritance, and that it supports interfaces, but not why.
    And from what I can see, the between multiple inheritance and single inheritance + interfaces make them seem almost equivalent, especially when you consider abstract classes. So why did the java designers make this decision?
    Edit: Just to say I've never programmed in an OO language that supports multiple inheritance, so I've never had to deal with it. Also, single inheritance has never crippled any of my designs (not that there have been that many), I'm not whingeing, just asking.
    Message was edited by:
    Dross

    Why does java not support multiple
    inheritance, but also give you the ability to use
    interfaces?It does support MI, just not MI of Implementation.
    why.
    class Beasty { }
    class Horse extend Beasty {
       public void gallop() { System.out.println( "horse" ); }
    class Donkey extend Beasty  {
       public void gallop() { System.out.println( "donkey" ); }
    class Mule extend House, Donkey {
    Mule mule = new Mule();
    mule.gallop();what would this print out.
    MI of implementation makes life harder, but adds very little to the party. So why add it?

  • Alternatives to multiple inheritance for my architecture (NPCs in a Realtime Strategy game)?

    Coding isn't that hard actually. The hard part is to write code that makes sense, is readable and understandable. So I want to get a better developer and create some solid architecture.
    So I want to do create an architecture for NPCs in a video-game. It is a Realtime
    Strategy game like Starcraft, Age of Empires, Command & Conquers, etc etc.. So I'll have different kinds of NPCs. A NPC can have one to many abilities (methods) of these: Build(), Farm() and Attack().
    Examples:
    Worker can Build() and Farm()
    Warrior can Attack()
    Citizen can Build(), Farm() and Attack()
    Fisherman can Farm() and Attack()
    I hope everything is clear so far.
    So now I do have my NPC Types and their abilities. But lets come to the technical / programmatical aspect.
    What would be a good programmatic architecture for my different kinds of NPCs?
    Okay I could have a base class. Actually I think this is a good way to stick with the DRY principle.
    So I can have methods like WalkTo(x,y) in
    my base class since every NPC will be able to move. But now lets come to the real problem. Where do I implement my abilities? (remember: Build(), Farm() and Attack())
    Since the abilities will consists of the same logic it would be annoying / break DRY principle to implement them for each NPC (Worker,Warrior, ..).
    Okay I could implement the abilities within the base class. This would require some kind of logic that verifies if a NPC can use ability X. IsBuilder, CanBuild,
    .. I think it is clear what I want to express.
    But I don't feel very well with this idea. This sounds like a bloated base class with too much functionality.
    I do use C# as programming language. So multiple inheritance isn't an opinion here. Means: Having extra base classes like Fisherman
    : Farmer, Attacker won't work.

    Hi
    PandoraElite,
    You can inherit from multiple interfaces (and use explicit interface implementation), but not from classes in C#. You can almost simulate it:
    In C# we don't support multiple inheritance
    http://blogs.msdn.com/b/csharpfaq/archive/2004/03/07/why-doesn-t-c-support-multiple-inheritance.aspx
    What would be a good programmatic architecture for my different kinds of NPCs?
    In your scenario, we can define some interface ,An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified
    in the interface definition.
    How to use? Please refer to the following article.
    http://www.codeproject.com/Articles/18743/Interfaces-in-C-For-Beginners
    Best of luck!
    Kristin
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • Multiple inheritance in Java

    Why it is sometimes said that interfaces provide a form of multiple inheritance?
    Do you agree that interfaces can provide multiple inheritance? Explain.
    Some people say that Java does not support multiple inheritance, and others: a class can implement more than 1 interface. Isn't that multiple inheritance?
    Thanks

    >
    Some people say that Java does not support multiple
    inheritance, and others: a class can implement more
    than 1 interface. Isn't that multiple inheritance?Sort of, but you don't inherit any implementation from an interface.

  • Replacement for multiple inheritance in MovieClip class

    Hello
    I need your opinion about a problem.
    Commonly, when you need to replace multiple inheritance, you do it by making an aggregation with one of the class involved.
    But in case of MovieClip attached class, there is one more class involved in the process : the MovieClip class, and this class can't be the one aggregated because every class attached to a MovieClip need to inherits from it.
    The problem is if the other class can't be also aggregated because it has some abstract class behaviour, for instance, call of a virtual function.
    A solution could be making the abstract class inherit from the MovieClip class, but what if you want to reuse its behaviour in a class which have nothing to do with MovieClip ?

    This is Not Supported in WebLogic that the Remote Interface extends other Interfaces. Because Annotation Processor just looks up inside the implemented interface methods. The actual interface which is Implemented by the Bean Class. So the Methods declared inside the Interface B and Interface C will be ignored and will not be available as part of the generated Stubs. Thats why u are getting NoSuchMethodError.
    You can even contact Oracle Support on this...there are 3-4 Cases on it. And the Solution is Work As Designed.
    Workaround is : edit your interface A as following
    Declare all the Business Methods only in the Remote Interface and not inside it's Super Interfaces.
    Example:
    @Stateless(name="A")
    @Remote({A.class})
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public class AImpl implements A {
    @Override
    public void writeA() {
    System.out.println("A");
    @Override
    public void writeB() {
    System.out.println("B");
    @Override
    public void writeC() {
    System.out.println("C");
    @Remote
    @JNDIName(A.JNDI_NAME)
    public interface A extends B, C {
    public static String JNDI_NAME = "A_JNDI_NAME";
    void writeA();
    void writeB();
    void writeC();
    Thanks
    Jay SenSharma
    http://jaysensharma.wordpress.com (WebLogic Wonders Are Here)

  • How does Java achieve multiple inheritance using interfaces

    Java does not allow multiple inheritance through classes as classes might contain methods with same names. what happens if a class implements two interfaces with same method names?
    I am really confused abt this? Can anybody help me out?
    Message was edited by:
    vijkris

    yes to avoid the ambiguous functions which can result due to multiple inheritance of classes like in c++ , java doesn't have this through classes. But if you have same method (both return type and parameter) then java doesn't bother and it won't complain as ultimately only one implementation is possible in deriving class even though method declalaration is there in both the interfaces. If return type changes then it won't compile as it can't overide the both methods as they have same name and different return types. thats why inside interfaces they restricted the implementation of methods so that it can work fine in ambiguous scenarios.

  • Hi All , Will Java supports Multiple Inheritance  classes???

    Hi All ,
    Will Java supports Multiple Inheritance by classes???
    Thanks in advance,
    Prakash

    No, Multiple inheritance would look like
    public class A extends B,C {(You can do that in C++, but it's rarely a good idea).That's not true at all. It's not inherently harmful, in C++ or any other language. It's entirely possible to do it correctly when it truly makes sense.
    Java just guarantees that nothing bad can happen to you by only allowing multiple inheritance of interface. You can't ever have multiple inheritance of implementation, that's all.
    %

  • More about multiple inheritance

    OK, you can solve problems where multiple inheritance is needed by using interfaces. But im facing a problem where it cant help me. Im constructing a system where there are componentes that need to extend JTextField as well Observable. I dont have interfaces above it in the hierarchy to substitute multiple inheritance. What can I do?
    When you have a scenario that you have to use two (or more) third party classes, and need to inherit from both, how do interfaces can help? If ate least I had multiple inheritance from classes...

    << Begin Rant >>
    I have seen more inherited code that is terribly designed because multiple inheritence was available.
    The example provided is a perfect example of this: At first blush, it seems easy to combine the UI and data components by combining Observable and JTextArea. If you were able to do this, the person inheriting your code in 3 years will curse your name.
    Nothing pisses me off more (well, I'm sure there are other things, but...) than attempting to debug C++ source code and finding that function calls are being made to multiple super classes.
    Here's a fun one: try adding an innocuous method getInfo() to a class you've inherited, only to find that someone uses getInfo() in one of the super-classes, and it has been declared as 'friend' because the design is piss poor and it was the only way they could make the function available. Now, I have to go on a goose chase searching for all the places in the entire type hierarchy that getInfo() is used and change the code to explicitly call the other base class.
    It gets to the point where its easier to name it getInfo2() (like that's good design) and get on with things.
    MI is evil, evil, evil in any environment where you are trying to have code re-use and multiple teams.
    I find that most programmers who insist that multiple inheritence is a good thing just don't know how to use the Composite design pattern.
    Sun's decision to not support MI in Java is a sound one: the result is code that can be easily read and understood.
    << End Rant >>
    Whew... I feel much better having said that...
    - K

  • Problems of no multiple inheritance.

    I have created two classes RECTANGLE with attributes Length and Height and PLANERECTANGLE, with various attributes required to specify the rectangle's center, an attribute that can be checked to see if it is inside an instance of rectangele. However, i am finding this following requirement difficult to understand.
         In Question 5, we specified PlaneRectangle as a subclass of Rectangle. Suppose that we wanted the following generic behaviour to be implemented in a number of different �kinds of� shapes: being able to move a shape, check if a point is inside a shape, and check if another shape lies completely inside a specified instance of some shape. Java will not let us do this using multiple inheritance. How else could we specify this? Rewrite the Java code to illustrate use of this different method.
    Thanks - Mark Costello.

    The answer would be an interface
    public interface Shape
    public void moveShape();
    public boolean containsPoint(int x, int y);
    public boolean containsShape(Shape s);
    Every shape class would then implement this interface:
    public class Circle implements Shape
    ... and would need to implement those methods that
    were specified (but not implemented) in the interface.

  • Why the multiple selection can't work in sub screen

    hi experts:
         when i try to use the sub-screen in the report,why the multiple selection function(SO_BCTXT) can't work normally in screen 1095:
    the coding as below.will reward points if solve this issue
    the main program in Subscreen:
    PROCESS BEFORE OUTPUT.
    module status_1005.
      CALL SUBSCREEN area INCLUDING sy-repid lv_tab_number.
    PROCESS AFTER INPUT.
      MODULE cancel AT EXIT-COMMAND.
      CALL SUBSCREEN area.
      MODULE user_command_1005.
    module user_command_1005 input.
      my_ok = ok_code.
      clear ok_code.
      case my_ok.
        when 'TAB1'.
          mytabstrip-activetab = my_ok .
          lv_tab_number = 1095.
        when 'TAB2'.
          mytabstrip-activetab = my_ok .
          lv_tab_number = 1096.
        when 'CRET' or 'ONLI'.
          perform get_bc_set
                 using
                    so_chanb[]
                 changing
                    <fs_bcsetfield> .
          leave to screen 0.
      endcase.
    endmodule. 
    in the screen 1095:
    PROCESS BEFORE OUTPUT.
    MODULE %_INIT_PBO_J.
    MODULE %_PBO_REPORT.
    MODULE %_PF_STATUS.
    MODULE %_SO_BCTXT.
    MODULE %_END_OF_PBO.
    PROCESS AFTER INPUT.
    MODULE %_INIT_PAI_J.
    CHAIN.
      FIELD  SO_BCTXT-LOW.
      FIELD  SO_BCTXT-HIGH.
      MODULE %_SO_BCTXT.
    ENDCHAIN.
    FIELD !INCBCTY1 MODULE %_INCBCTY1 .
    FIELD !INCBCTY2 MODULE %_INCBCTY2 .
    FIELD !INCBCTY3 MODULE %_INCBCTY3 .
    FIELD !INCBACT MODULE %_INCBACT .
    FIELD !INCBINAC MODULE %_INCBINAC .
    FIELD !INCLMAND MODULE %_INCLMAND .
    FIELD !INCLOPTI MODULE %_INCLOPTI .
    CHAIN.
      FIELD  SO_BCTXT-LOW.
      FIELD  SO_BCTXT-HIGH.
      FIELD INCBCTY1 .
      FIELD INCBCTY2 .
      FIELD INCBCTY3 .
      FIELD INCBACT .
      FIELD INCBINAC .
      FIELD INCLMAND .
      FIELD INCLOPTI .
      MODULE %_BLOCK_1095001.
    ENDCHAIN.
    CHAIN.
      FIELD  SO_BCTXT-LOW.
      FIELD  SO_BCTXT-HIGH.
      FIELD INCBCTY1 .
      FIELD INCBCTY2 .
      FIELD INCBCTY3 .
      FIELD INCBACT .
      FIELD INCBINAC .
      FIELD INCLMAND .
      FIELD INCLOPTI .
      MODULE %_END_OF_SCREEN.
      MODULE %_OK_CODE_1000.
    ENDCHAIN.
    in the main report
    selection-screen begin of screen 1095 as subscreen.
    enquire Data
    data l_text       type as4text.
    selection-screen begin of block b1 with frame title text-024.
    select-options :
      so_bctxt     for l_text .
    selection-screen skip 1 .
    parameters:
      incbcty1    type c as checkbox default abap_true ,
      incbcty2    type c as checkbox default abap_true ,
      incbcty3    type c as checkbox default abap_true .
    selection-screen skip 1 .
    parameters:
      incbact      type c as checkbox default abap_true ,
      incbinac     type c as checkbox default abap_true .
    selection-screen skip 1 .
    parameters:
      inclmand     type c as checkbox default abap_true ,
      inclopti     type c as checkbox default abap_true .
    selection-screen end of block b1.
    selection-screen end of screen 1095 .

    Hiii
        In main program you have dclared subscreen area.
    now First of all you have to declare a selection screen in your main program
    selection-screen begin of screen 1005 as subscreen.
    SELECT-OPTIONS s_bukrs1 for  v_bukrs.   "whatever you want in selection screen
    selection-screen end of screen 1005.
    IN PBO of your main screen (not in 1005) write
    call subscreen SUB1 including sy-repid '1005'.
    where SUB1 is your subscreen area name. And remember don't touch PBO or PAI of your 1005 screen, otherwise it will give you error. Yuo can just put text in layout of 1005 screen. but don't disturb other things.

  • How java support multiple inheritance by the use of interface.

    As per my understanding, Interface is just having the signatures of the methods not the implementation.
    So How java support multiple inheritance by the use of interface?
    Answer 1: we can institate interface reference by its implemented
    class.
              ����� interface inf...
              ����� class aa implements inf..
              ����� class bb implements inf....
               Now, inf i = new aa();
               inf i = new bb();
    Answer 2: We can extends as many interface as we want in the
    single
               interface.
               i.e. interface infFirst....
               interface infSecond....
               interface infThird....
               Now ,
               interface ingMulti extends infFrist, infThird...
    By above two answers its not prity clear as per the multiple inheritance in C or C++.
               i.e.
               class first{
               method abc();....}
               class second{
               method bbc()......}
               class multi::first::second{
               we can call to abc();.....as well as bbc();
    -Please give your important suggstion on the same.(Hope I explain it well.)
    -Jeff

    The keyword implement is used only for interfaces not
    for abstract class. If i am wrong correct me.I believe your right, but I will double check.
    As for the multiple inheritence think about the following code:
    class Animal {
        //  Animal generic stuff in this class
    interface Eat {
        //  Generic stuff that models eating behavior
    interface Runs {
        //  generic methods that model running behavior
    public class Horse extends Animal implements Eat, Runs {
        //  Stuff specific to a horse
    }The Animal class is generic but has stuff in it common to all animals.
    The Eat interface models behavior that is generic to eating, all living things have to eat something to survive. Herbavore are different from carnivores.
    The Runs interface models generic behavior to running, such as speed. A cheeta definately runs faster than a human.
    This brings us to the Horse class. It extends the Animal class because it "is-a" animal, and it implements the eat and runs interface because they are behaviors a horse has.
    I hope that helps.
    Extending an abstract class is the same as extending a regular class with the exception you MUST override all abstract methods in the abstract class. Thats not too difficult but I believe when designing classes, designing an abstract can be more diffecult than modeling the base class, and generic behaviors in interfaces. JMO.
    JJ

  • Multiple inheritance in tagging interface? Is it possible?

    I saw a code somewhere that goes like this:
    public interface Node extends Serializable, Clonable
    ...Is it possible? I know that Java doesn't allow multiple inheritance and that Serializable and Clonable are tagging interfaces where no method must be implemented by the programmer.

    KamenRiderZX wrote:
    I know that Java doesn't allow multiple inheritanceMore exactly: Java doesn't allow multiple inheritance of implementations. Inheriting multiple interfaces ("implements" for classes, "extends" for interfaces) is fine 'though.

Maybe you are looking for