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

3
  • Object --> Animal --> Dog. This is the hierarchy. I don't see multiple inheritance here. Its only multi-level. Commented Nov 6, 2013 at 4: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. Commented Nov 6, 2013 at 4:57
  • Here the answer to your question Commented Nov 6, 2013 at 5:00

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.