Multiple inheritance question

This just occurred to me. I read the other day that all classes inherit Object. So if I have a class defined as public class A {
}and I call toString() on an instance of this class, I get Objects toString implementation (assuming it has not been overridden). OK, this I understand.
Now if instead I havepublic class A extends B {
}and I call toString() on A, assuming it is not overridden in either A or B, I will still get Objects toString method. My question is, does A still extend Object, or does it inherit the fact that B extends Object? And what happens if I override toString() in B - what will I get if i call toString on an instance of A?

java.lang.Object is at the root of all class inheritance hierarchies in Java. Therefore, all classes inherit from java.lang.Object, either directly or indirectly.
When you call a method on an object of class Foo, Foo's inheritance hierarchy is traced starting with Foo and going all the way back to java.lang.Object, looking for an implementation of the method. The first implementation encountered is the one that gets used. So in your example calling toString on an instance of class A will result in B's toString method being executed.
Hope this makes sense.

Similar Messages

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

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

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

  • Interfaces instead of multiple inheritance?

    I've read that "The Java programming language does not permit multiple inheritance , but interfaces provide an alternative."
    But I also read contradictory information-There are no method bodies in an interface.
    Java interfaces only contain empty methods? Apparently, if I want to share a method among classes, I have to re-write the methods in each class that implements the interface. That doesn't seem at all like multiple inheritance. Am I missing something?
    It seems that I will have to cut and paste the implementation code from one class to another, and if I change the methods, I have to cut and paste it all over again.
    I've read that interfaces save a lot of time re-writing methods, but how?
    Does this really provide the same capabilities as multiple inheritance, or am I missing something?
    Thanks,
    Pat

    Pat-2112 wrote:
    I've read that "The Java programming language does not permit multiple inheritance , but interfaces provide an alternative."
    But I also read contradictory information-There are no method bodies in an interface. That's not contradictory.
    Inheritance is about type, which interfaces provide. It is NOT about sharing code, which is all that's lacking by not having multiple inheritance of implementation.
    Java interfaces only contain empty methods? Apparently, if I want to share a method among classes, I have to re-write the methods in each class that implements the interface. That doesn't seem at all like multiple inheritance. Am I missing something? Yup. You're missing the point of inheritance, and the fact that delegation allows you to use an implementation defined in one class in another class.
    It seems that I will have to cut and paste the implementation code from one class to another, Nope.
    public interface Cowboy {
      void ride();
      void draw();
    public interface Artist {
      void sculpt();
      void draw();
    public interface CowboyArtist extends Cowboy, Artist {
    public class CowboyImpl implements Cowboy {
      public void ride() {
       System.out.println("Giddyup!");
      public void draw() {
        S.o.p("Bang!");
    public class ArtistImpl implements Artist {
      public void sculpt() {
        S.o.p("Demi Moore in Ghost. Yum!");
      public void draw() {
        S.o.p("Sketch a picture of a gun.");
    public class CowboyArtistImpl implements CowboyArtist { // or implements Cowboy, Artist
      private final Cowboy cowboy = new CowboyImpl();
      private final Artist artist = new AristImpl();
      public void ride() {
        cowboy.ride();
      public void sculpt() {
        artist.sculpt();
      public void draw() { // uh-oh, what do we do here?
        artist.draw();
        cowboy.draw();
    }The draw method is not relevant to this particular question. It's an example of one of the problems with MI, and I just included it since it usually comes up int these discussions anyway. Ride and sculpt demonstrate the point about delegation.

  • Multiple-inheritance semantics

    I saw the other day multiple-inheritance is possible with JavaFX classes.
    class X extends Y,Z,A {
    Something which is possible now as JavaFX classes don't have constructors. Now we can mix-in (as in Scala) code from other classes.
    My question to the people who design this is about their intention: is it meant to be used as a composition ? Or still as a specialization like it is in Java.

    If you allow multiple inheritance you get some ambiguities. Java avoid those by having single inheritance of implementation (one class may extend one class only) in addition to multiple inheritance of interfaces (that don't implement anything). This is much cleaner and that was one of Javas design objectives. The goal is to be safer than C++ and this often means leaving things out, like calls by reference, pointer arithmetics and multiple inheritance.

  • Best app to import and study multiple choice questions.

    Hi,
    I'm searching for an app to help study for an exam.
    Part of the studying strategy is to study by the previous exams questions. I have those (a lot of them) in text format.
    The app would need to have:
    - Support for multiple-choice questions;
    - Easy, fast and automated way of importing the questions;
    Would be nice to have:
    - Ability to include a rich-text comment along with the right/wrong answer;
    - Support for tagging each questions.
    I've used the search function and found this:http://forums.macrumors.com/showthread.php?t=1041085 but it's been a while since then.
    thanks in advance

    Hi Konrad;
    What approach would you recommend to import and export EBS data? Open Interfaces, PL/SQL, web services or anything else?For your question please check below notes:
    Export/import process for R12 using 11gR1 [ID 741818.1]
    Export/Import Process for Oracle E-Business Suite Release 12 using 10gR2 [ID 454616.1]
    General Notes For E-Business Suite Release 12 [ID 986673.1]
    Please also check below thread:
    how to export data from ebs
    how to export data from ebs??
    Also check below search which could help you about your issue:
    http://forums.oracle.com/forums/search.jspa?threadID=&q=import+export+&objID=c3&dateRange=all&userID=&numResults=15
    http://forums.oracle.com/forums/search.jspa?threadID=&q=export+data&objID=f475&dateRange=all&userID=&numResults=15&rankBy=10001
    Hope it helps
    Regard
    Helios

  • Multiple Choice Question Issues

    I am embedding a quiz within my presentation with individual
    question slides interspersed throughout the presentation. These
    questions are pulled from a couple of different question pools. I
    want to use a multiple choice question that branches to different
    parts of the presentation depending on the correct or incorrect
    answer. If the user answers the question incorrectly, I send them
    to the next slide which has two click boxes. On this slide, they
    can either click one box to move back in the presentation and
    review the material or click another box to move them forward to
    another multiple choice question. If they go back and review the
    information, they would eventually return to the same multiple
    choice question to try it again. (This is the desired navigation,
    anyway.)
    What actually happens is that when the user goes back to
    review the information and then finally returns to the question,
    the question has not been reset and still shows the incorrect
    answer marked with no way to clear it.
    I have used this same navigation on a drag-and-drop type
    question and it works fine. All the settings for the
    multiple-choice questions are set the same as in the drag-and-drop
    question.
    Any help in making this work correctly would be greatly
    appreciated.

    I can't see where that error might arise except possibly for a case where an AS2 radio is used since it appears they don't have a group property, but an AS3 radio button does.  Check to be sure that your Flash Publish Settings are set to use AS3.  AS3 errors normally have error numbers which also makes the error you quoted ring of an AS2 scenario.
    Here's a link to the file I made based on your code that works fine and doesn't display the errors you mention.  It is a CS3 file.
    http://www.nedwebs.com/Flash/AS3_Radios.fla

  • Multiple choice question in intractive pdf?

    Hi,
    I have a question about Multiple option question. How to create Multiple option single answer question with explanation popup in interactive PDF. Please guide me.
    Regards
    Anil

    Yes, this worked fine.
    I just had to place the animal to the left of the table and just reduce it slightly in size. It worked fine.
    Thanks!

  • 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

  • Select more than one answer on a multiple choice question

    How do you select more than one answer on a multiple choice question on an employment application on the internet?

    marmaldonado,
    Is this a PDF form? Or a web form?
    Clinton

  • 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

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

  • Multiple Inheritance

    Hello,
    I have been programming Java for last year,
    evolved in quite some skills with it, and
    really think it is great...
    However, I was shocked to find out that there
    is no multiple inheritance feature.
    I know it is rare, and my case proves it
    (1 year now, I never needed it)
    HOWEVER, when one needs multiple inheritance,
    then they really do need it.
    I have interfaces which I would like implemented
    in their respective class (ie ISomething be
    implemented in CSomething), then some classes
    I need to implement many of those interfaces...
    Now I am forced to have those classes extend
    multiple interfaces, and duplicate the interface
    implementation code inside each of them.
    I dont mind a little bit of copy/paste, nor
    do I care about the compiled classes being
    slightly bigger, BUT the problem is that
    when I need to change some behaviour in those
    interfaces, in the near (or far) future, I will
    have their implementation scattered in many
    classes... This is dangerously error prone and
    not proffesional at all... And I do not think
    that including multiple inheritance in the language
    could be more error prone than this...
    I think the Java team does a 100% perfect brilliant
    job, but at this specific point, they "over-tried"
    to "protect" the programmers from themselves...
    Well, thats all,
    I think some next version of Java should support
    multiple inheritance. And the Java "warning" could be :
    "if you havent missed it till now, then you probably
    do not need anyway, so do not bother using it just
    because it exists"
    Thanks for reading my thoughts,
    Dimitris

    Personally I never need multiple inheritance of code and I try to avoid inheritance of code whenever possible. A common mistake in OO is too use inheritance as a way of reusing code. Code reuse is much easier, cleaner and more powerful by using composition instead. Only use inheritance for polymorhism (to use multiple implementations for the same interface). An example:
    interface A {
      void ma();
      void maa();
    interface B {
      void mb();
    class C implements A, B {
      private A a;
      private A c;
      private B b;
      public void ma() {
        a.ma();
      public void maa() {
        c.maa();
      public void mb() {
        b.mb();
    }This is much more powerful than code reuse through inheritance. In this example I use one method from 'a' and one method from 'c' when I implement interface A. I can change the value of 'a', 'b' and 'c' during runtime, and I dont have to reuse all the code in 'a' and 'b', I can select which code to reuse. This is the power of composition and interfaces. Note that I only access 'a', 'b' and 'c' through the interfaces A and B, never directly through their implementations.
    I would recommend you to look at your design and start to think about interfaces and inheritance, not about code reuse though inheritance.

Maybe you are looking for

  • Need HELP installing XP Pro on two 74gb Raptors on Neo2 Plat!!!!!

    My friend and I have tried almost everything. We tried them in SATA1/2 and SATA 3/4. We made sure everything that should be disabled/enabled in BIOS is. We followed the guides on this forum. When we get to Windows Setup, we hit F6, and then when it a

  • Data transfers from SRM to R/3

    Hi, i have follwing questions? 1. How date transfer from SRM to R/3? 2. Why PO transfer throught RFC? why not IDoc? and why confirmation and invoices are transfer using IDOC?. 3. config document for EBP-SUS? Thanks Rams

  • Does OC4J support "LIKE" operation in ejb-ql?

    I have a sql in ejb-jar.xml: <query>           <query-method>                <method-name>findByZoneidTypeLnameInfo1</method-name>                <method-params>                     <method-param>java.lang.String</method-param>                     <m

  • AT USER-COMMAND IN NORMAL REPORT

    Please do not have your subject in ALL CAPITALS Hi Experts, Please advice, I am having three fields in output first one  is directly fetched from table and other two are variables. ITAB-MENGE = fetched data. ITAB-UCOMM =   user command. AT SELECTION-

  • Tableless JSP no displaying correctly.

    I have a Tableless JSP that works fine in JDeveloper 10.1.2, but in 10.1.3 J2EE Edition, the Visual JSP designer doesn't show the page right. Are there any issues regardingthis on 10.1.3? Thanks.