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

Similar Messages

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

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

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

  • 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

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

  • Inheritance Concept Explanation

    Hi,
    Java does not support multiple inheritence (atleast extending multiple classes).
    I wanted to know why java has come up with such idea which violates OOPS concept?
    I know that there can be confliction with overriding methods with same name and signature from different classes. But this might occur once in centuries cases, and not even that. Then why java has ommited such a beautiful concept?
    Definitely there has to be some other reason.
    Please discuss on it.
    Thanks and regards
    Gopal Krishan Sharma
    [email protected]
    [email protected]

    > i have been taught that think of java as the
    implementers of OOPS...
    Not by a long shot. OOP predates Java by roughly thirty years.
    > Tell me what could be the possible harm in this case...
    -- BEGIN QUOTE
    Interfaces and the 'diamond problem'
    One justification of interfaces that I had heard early on was that they solved the "diamond problem" of traditional multiple inheritance. The diamond problem is an ambiguity that can occur when a class multiply inherits from two classes that both descend from a common superclass. For example, in Michael Crichton's novel Jurassic Park, scientists combine dinosaur DNA with DNA from modern frogs to get an animal that resembled a dinosaur but in some ways acted like a frog. At the end of the novel, the heros of the story stumble on dinosaur eggs. The dinosaurs, which were all created female to prevent fraternization in the wild, were reproducing. Chrichton attributed this miracle of love to the snippets of frog DNA the scientists had used to fill in missing pieces of the dinosaur DNA. In frog populations dominated by one sex, Chrichton says, some frogs of the dominant sex may spontaneously change their sex. (Although this seems like a good thing for the survival of the frog species, it must be terribly confusing for the individual frogs involved.) The dinosaurs in Jurassic Park had inadvertently inherited this spontaneous sex-change behavior from their frog ancestry, with tragic consequences.
    This Jurassic Park scenario potentially could be represented by the following inheritance hierarchy:
              Animal
           Frog   Dinosaur
             FrogasaurThe diamond problem can arise in inheritance hierarchies like the one shown in Figure 1. In fact, the diamond problem gets its name from the diamond shape of such an inheritance hierarchy. One way the diamond problem can arise in the Jurassic Park hierarchy is if both Dinosaur and Frog, but not Frogosaur, override a method declared in Animal. Here's what the code might look like if Java supported traditional multiple inheritance:
    abstract class Animal {
        abstract void talk();
    class Frog extends Animal {
        void talk() {
            System.out.println("Ribit, ribit.");
    class Dinosaur extends Animal {
        void talk() {
            System.out.println("Oh I'm a dinosaur and I'm OK...");
    // (This won't compile, of course, because Java
    // only supports single inheritance.)
    class Frogosaur extends Frog, Dinosaur {
    }The diamond problem rears its ugly head when someone tries to invoke talk() on a Frogosaur object from an Animal reference, as in:
    Animal animal = new Frogosaur();
    animal.talk();Because of the ambiguity caused by the diamond problem, it isn't clear whether the runtime system should invoke Frog's or Dinosaur's implementation of talk(). Will a Frogosaur croak "Ribbit, Ribbit." or sing "Oh, I'm a dinosaur and I'm okay..."?
    The diamond problem would also arise if Animal had declared a public instance variable, which Frogosaur would then have inherited from both Dinosaur and Frog. When referring to this variable in a Frogosaur object, which copy of the variable -- Frog's or Dinosaur's -- would be selected? Or, perhaps, would there be only one copy of the variable in a Frogosaur object?
    In Java, interfaces solve all these ambiguities caused by the diamond problem. Through interfaces, Java allows multiple inheritance of interface but not of implementation. Implementation, which includes instance variables and method implementations, is always singly inherited. As a result, confusion will never arise in Java over which inherited instance variable or method implementation to use.
    -- END QUOTE
    http://www.javaworld.com/javaworld/jw-12-1998/jw-12-techniques.html

  • 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

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

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

Maybe you are looking for

  • The top 8 main issues in v5.3 that has to be fixe...

    1- it takes too long to actually start ringing on the other person's device or in another way it takes too lonh saying connecting before starting to ring 2- the status of contacts are meaningless specially the away status which doesn't indicate anyth

  • Swapping hard drive into external enclosure

    Hello. I just installed a hitachi 160gb hard drive in my macbook. No problems, easy as pie. So I wanted to install the original toshiba 60gb into a usb external enclosure, here's the problem. So the hard drive has a plastic part on it that covers the

  • Regarding campus management

    Hi experts    please explain me what is HE &R AND what is campus management explain me the  business flow in  HE &R and campus management & modules thanks in Advance Avi

  • Getting warning message when running BW Report

    Hi , When i am running saved query iam getting warning message pop up and when iam clicking ok button iam getting data. My query is there is any way to remove that warning message pop up message? Thanks in advance

  • HT1766 iphone recovery wasn't successful due to low disc space. should i erase now?

    upgraded iphone 4 to iOS7.   Phone is now disabled as it was asking for a code and neither my granddaughter nor I knew what it was (I believe it should have been 1234).   Have tried recovery but it wasn't successful due to low disc space.   Im no tec