-2

In java they are saying that class can not be inherited by multiple super classes. Only can be one. See this code,

Animal.java

public class Animal {   }

Dog.java

public class Dog extends Animal{

public static void main(String[] args) {

    Animal animal = new Animal();
    Dog bulldog = new Dog();
    Dog rottweiler = new Dog();



    if (bulldog.equals(rottweiler)) {

        System.out.println("animal == bulldog");

    }else {

        System.out.println("animal != bulldog");
    }

}    } 

Hear We can see that Dog class is inheriting from Animal. But I am using equals method which belongs to Object class in java. In SCJP book they are saying that each and every class in java is a subclass of Object class. Yes..! That is why I am able to use methods like equals in Dog class. Then my question is, aren't we unknowingly using multiple inheritance in java? Why because we are inheriting from Object and Animal classes to Dog class?

CodeSac
  • 311
  • 2
  • 4
  • 12
  • `Object --> Animal --> Dog`. This is the hierarchy. I don't see multiple inheritance here. Its only multi-level. – Rahul Nov 06 '13 at 04:56
  • Nope! `Dog` inherits from `Animal`; `Animal` inherits from `Object`. That's single inheritance. The inheritance "cascades" in a way, but that is not multiple inheritance. – Richard JP Le Guen Nov 06 '13 at 04:57
  • [Here the answer to your question](http://stackoverflow.com/questions/9790435/java-doesnt-support-multiple-inheritance-but-implicitly-every-class-in-java-ext) – Aneel Ansari Nov 06 '13 at 05:00

4 Answers4

3

In this case, Animal extends Object; and Dog extends Animal. Hence, it is single inheritance anyways.

The equals() method is defined in Object, and it becomes accessible to Dog via Animal

enter image description here

aquaraga
  • 4,138
  • 23
  • 29
0

Java supports multi-level inheritance but not multiple inheritance.

  • multi-level inheritance : C extends B, B extends A (legal)
  • multiple inheritance : C extends B,A (not legal)

So in your case Dog extends Animal and Animal extends Object, which is legal.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

Every class in Java (except Object) has exactly one immediate superclass. If you had a class called Rottweiler in your example, then the immediate superclass of Rottweiler would be Dog, the immediate superclass of Dog would be Animal and the immediate superclass of Animal would be Object. Although Rottweiler has three superclasses (Dog, Animal and Object), and can inherit members from all three of those, it only has one immediate superclass.

If we had multiple inheritance, we'd be able to do things like define Dog to extend both Animal and Companion. But we can't do that in Java.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
0

That is called "Multilevel inheritence" not multiple inheritence.

Consider the examples below:

Example 1:

class Foo { // Here you haven't specified any extends, so internally it will be
            // from Object class
}

Example 2:

class Bar extends Foo { //Here you have Foo as parent class
                        //Now Bar has all of the methods which are accessible
                        //from Foo as well as Object which is a parent of your
                        //class "Foo"

}

This is what you are assuming:

class Bar extends Foo,Object { //No this is not how it works

}

Hope it helps you.

Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107