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.

Similar Messages

  • No multiple inheritance in Java. Interfaces used.

    Hi,
    In java a class can extend only one class while the interface can extend any number of interfaces.
    Class extending only one class avoids multiple inheritance.
    Can you explain me the reason of avoiding this in classes and allowing interfaces to extend any number of interfaces ?

    Hi,
    In java a class can extend only one class while the
    interface can extend any number of interfaces.
    Class extending only one class avoids multiple
    inheritance.
    Can you explain me the reason of avoiding this in
    classes and allowing interfaces to extend any number
    of interfaces ?The real question is: do you have a need for multiple inheritance?
    If so, I would be glad to hear about this concrete problem.

  • Simulating Multiple Inheritance in Java

    Problem:
    I am trying to obfuscate code and create wrapper classes to access code implementation. Problem is that some parameters in methods have different types. (i.e.
    public class Example{
    ExampleImpl exampleImpl;
    public void setExample(Problem exam) {
    exampleImpl.setExample(exam); //complile error
    //error message: problemImpl is not of type problem
    public class ExampleImpl {
    public void setExample(ProblemImpl exam) {
    //do something
    The objects Problem and ProblemImpl are abstract class that provode some implementation.
    I know I have to sumulate multipule inheritance but its really have to changed the abstract classes to inherate each other. Problem with that is the ProblemImpl abstract class will be obfuscated.
    Any suggestions.

    Not quite following you but perhaps
    java.lang.reflect.proxy
    could be of service

  • Doubt on Multiple Inheritance

    Hi all,
    I am confused by the java statement that "By using interface we can achieve multiple inheritance bcoz java doesn't support Multiple inheritance".
    Yes.Ok java doent support Multiple inheritance. Now we know that inheritance means that "one object acquires the property of another object".
    So how can it is possible achieve Multiple inheritance by interface.
    Interface that contains just undefined methods like below code.
    interface Member1
         public void eye();
         public void nose();
         public void mouth();
    interface Member2 extends member1
         public void neck();
         public void hand();
         public void stomach();
    interface Member3 extends Member1,Member2
         public void leg();
    class Man implements Member3
         public man()
         Member3 ref=new Man();
         // Here Implements all 7 methods.
    Is the above code defines multiple Inheitance?
    undefined methods are eye,nose,mouth,neck,handand stomach are fall in Interface Member3 .Yes. But Inheritance means that one object acquires the property of another object.Property means that code and data.
    In here, there is no code just declarations of method which is not to be a code .
    So How can we say interface achieve multiple inheritance.
    Please any one explain and clear my doubt with simple example.
    with cheers,
    G.GandhiRaj.

    Multiple inheritance is about aquiring both behavior and attributes from two or more sources. A lot of times, this "is a" relationship is confused with "has a" relationships.
    For example, a Book "has a" Page. A Book "is a" Publication. So multiple inheretance in this instance would come from stating that a Book "is a" Publication and "is a" PaperProduct. In this example, you could redesign your model and state that a PaperProduct inherits from Publication. However, a Book doesn't have to be limited to being a PaperProduct, it can also be an ElectronicProduct, thus inhereting attributes and behaviors from this new class as well. In essence, the Book can exist in two forms simulataneously (as many actually do). So you still have the need for multiple inheritance - perhaps.
    Interfaces define the behavioral aspects of multiple inheritance. You loose the aquisition of attributes from base classes. In many cases this is acceptable. For the first time I recently found a true need for multiple inheritance in one fo my Java apps. Some would say that my data model is poorly designed then. So I tried restructuring the model and that solve the problem.
    It is probably a good idea to completely understand your data model from many dimensions first before resorting to the copying of attributes like I almost did. Don't be locked into a design too quickly. Be willing to change your mind and you will probably find a solution that works.

  • Multiple Inheritance of Class problem

    I want to inherit two classes in my class
    Since java does not support multiple inheritance of java classes , only can implement java interface , how can I achieve this .

    I too have a dream.
    Someday I want to see a design that uses multiple inheritance correctly. And as long as I am dreaming it might as well be a design that is not dependent on legacy applications.
    I strongly suspect that it will forever remain a dream.

  • 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

  • Enumerate multiple inheritance scenarios and their java equivalents?

    hi,
    ppl have often said things like
    "java doesn't support multiple inheritance but this is ok, since there are other mechanisms for doing the same thing (by which we nearly always mean interfaces)"
    how solid a statement is this? are there any formal methods available eg smt like "over all possible inheritance trees over all possible classes, only a handful of cases are distinct when viewed from the converting-to-single-inheritance scheme"?
    the two things mentioned as harder to workaround are mixins and the diamond problem - are there more?
    also what other mechanism apart from interfaces (if any) are useful?
    any help appreciated,
    asjf

    What I say is that it doesn't matter since there is
    almost never any need for MI. Most of the time it is
    used it is used because the developer/designer did not
    understand what they were doing and it should not have
    been used in the first place.
    That leaves one with very few cases where it should
    ever be used. And that coupled with the fact that a
    developer should never use it unless they are very
    experienced (so that they actually know that it should
    be used,) means that practicing programmers should
    leave discussion of such usages to scholarly
    journals.thanks :) I guess my problem is that often with computer stuff you don't have to rely on other peoples experience about things - you can go and test it yourself
    I've done very little C++ development, and so have never come across real-world multiple inheritance. I bumped into the first situation with some java code where it might've been a neat solution recently but this could easily fit into the "designer did not understand what they were doing" category from above..
    will have a casual look around the scholarly journals if I can find any that look promising :)
    asjf

  • No Calrity on Multiple Inheritance Concept Of Java..!

    In SCJP Book by "karty serie " .
    In Java
    a subclass of class Object, (except of course class Object itself). In other words, every
    class you'll ever use or ever write will inherit from class Object. You'll always have
    an equals method, a clone method, notify, wait, and others, available to use.
    Whenever you create a class, you automatically inherit all of class Object's methods.
    A class cannot extend more than one class. That means one parent per class. A
    class can have multiple ancestors, however, since class B could extend class A, and
    class C could extend class B, and so on. So any given class might have multiple
    classes up its inheritance tree, but that's not the same as saying a class directly
    extends two classes.
    class PlayerPiece extends GameShape, Animatable { // NO!
    // more code
    If the above code is invalid, is it legal to write the code like ...
    class PlayerPiece extends GameShape, Object { // NO!
    // more code
    Thanks In Advance
    Kiran

    I think I can help straighten out what is confusing you.
    Let's say you have a class B that extends class A, and a class C that extends class B.
    Then class C implicitly extends class A, but java does not allow you to make that explicit.
    So, yes, in a way, class C does subtype both class A and B, but it only directly subclasses class B. The subtyping of class A is implicit.
    The following should demonstrate some patterns that should help clear things up:
    class A { } 
    // This automatically is a subclass of Object,
    // you don't need to specify that.
    // or
    class A extends Object { } 
    // This is legal, but not necessary, since a class
    // that doesn't extend anything else implicitly extends Object
    class B extends A { } 
    // This directly subclasses class A,
    // and implicitly subtypes Object
    // but
    class B extends A, Object { } 
    // This is NOT legal in java, and will not compile,
    // even though the previous code would
    // make class B a subtype of Object
    class C extends A { } 
    // Again, a direct subclass of A,
    // and indirect subclass of Object
    class D extends B, C { } 
    // This is NOT legal in java, and is what people
    // usually mean when they say that multiple
    // inheritance is prohibited.  In this case, you
    // are attempting to subclass two different
    // classes, where one is *not* a subtype of
    // another.  There is no work around to make
    // this happen in java, but you can use interfaces
    // and composition to get a similar effect with a
    // bit of glue code to hook things together.
    // For example:
    interface X {
      public void doX();
    class XImpl implements X {
      public void doX() {
        // do something
    interface Y {
      public void doY();
    class YImpl implements Y {
      public void doY() {
        // do something else
    class Z implements X, Y {
      private X x = new XImpl();
      private Y y = new YImpl();
      public void doX() {
        x.doX();
      public void doY() {
        y.doY();
    // This is basically what goes on behind the scenes
    // in languages like C++ that do support MI

  • 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.

  • 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.
    %

  • What is single inheritance, multiple inheritance, and describe Java's notio

    What is single inheritance, multiple inheritance, and describe Java's notion of an interface?
    Can you give me example or reference link? thx

    Single inheritance is getting features like data and methods (functions) from a so called parent class. multiple inheritance is the same but you derive features from multiple parent classes (not supported by java). Interfaces are a way around this because you can inherit multiple interfaces. Inheriting from interfaces is like a promise to implement certain methods that these interfaces define but doesn't implement themselves.
    check around java.sun.com in the tutorials section, you can probably find a text that describes object oriented program and how it is implemented in java.

  • 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.

  • 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

Maybe you are looking for

  • Warning messages (Moving from JDK1.4 to DJK1.5)

    I got some warning messages after I moved from 1.4 to 1.5, can someone show me the right way to avoid them ? ===================================================================== Scanner.java:909: warning: [unchecked] unchecked call to add(E) as a me

  • Upgrade Oracle database 10.2.0.1 to 10.2.0.2 on windows

    Hi All, I have installed Oracle Database 10g 10.2.0.1 on windows XP.Right now I want to Upgrade it by 10.2.0.2. For that can any one plse suggest me the xact patch no or any document. Urgent required. Thanks Rishikesh

  • How do I get calendar to compute travel time in Mavericks

    I love the new feature in Mavericks where the calendar will automatically compute your travel time to an event.  However, I can't get it to do that.  I have a dropdown box option but it isn't figuring it for me.  We are moving to a new area, and I do

  • Won't create Excell sheet accurately

    I have reports done in Excell with 4 sheets as pages. they are all pretty much the same lay-out. All pages show normally when looked at under print preview for Excell but only 2,3,and 4 convert to Adobe properly. (I'm using 6.0 Standard). The bottom

  • BB not syncing with Outlook 2011 Mac

    Hi,  Up until not long ago my BB Torch was syncing without a problem with my Outlook 2011 Calendars in my Mac. Now for some reason it has just stopped working! I did have a few problems with iCloud wiping all of my data off the calendars, iCloud has