13

Fact 1:

Java does not support multiple inheritance.

Fact 2:

Object is a superclass of all other classes

If I have a class Parent and a class Child which is inheriting the class Parent:

class Parent {

}

class Child extends Parent {

}

In this case, how will the class Child inherit the Object class, if Java does not support multiple inheritance?

How is the relationship between these three defined?

Option 1:

enter image description here

Option 2:

enter image description here

21
  • Object is the superclass of every class in Java.
    – deHaar
    Commented Aug 31, 2018 at 9:43
  • 1
    @Arvind multilevel inheritance and multiple inheritance are not the same things. Java supports the multilevel inheritance, but C++ does both. Commented Aug 31, 2018 at 10:02
  • 1
    please don't post answers/solutions in questions but feel free to write a new answer instead and mark it as accepted if you may think that's the real solution to your problem. See stackoverflow.com/help/self-answer Commented Aug 31, 2018 at 10:46
  • 1
    @AbhishekKeshri yes you can - just use the answer box below. All "self-answer" does is display an answer box on the question field. But do you need to post an answer at all? Seems like there is already a perfectly good answer, which you have accepted.
    – Ant P
    Commented Aug 31, 2018 at 11:53
  • 1
    @AntP I will remove the Solution part. Your argument makes sense. Commented Aug 31, 2018 at 12:02

10 Answers 10

26

It's Option 2. If you define a superclass, that will be the immediate superclass of your class. If you don't define one, Object will be the immediate superclass.

class Parent {

}

class Child extends Parent {

}

is equivalent to

class Parent extends Object {

}

class Child extends Parent {

}

So, while Object is the superclass of all classes, there might be some steps in the class hierarchy before you get to Object. It's not the immediate superclass of all classes.

2
  • Won't class Child also inherit Object? Won't it be class Child extends Parent, Object (if it's possible internally somehow!!!)? Commented Aug 31, 2018 at 9:46
  • 6
    @AbhishekKeshri No. Child inherits Object through Parent. Commented Aug 31, 2018 at 9:47
9

Object might not be a direct parent, but it's always a super parent.

Child extends Parent
Parent extends Object

 |
 V

Child [indirectly] extends Object
1
  • @AbhishekKeshri if the scenario 1 were correct, we would encounter the problem of choosing methods for the subclass (since both Object and Parent extends Object have the same set of methods including equals, hashCode, etc.) Commented Aug 31, 2018 at 9:59
7

The JavaDoc says:

Class Object is the root of the class hierarchy. ...

If a class does not extend any other class by decalring it using the keyword extends it extends though implicit from Object.

The documentation says:

In the absence of any other explicit superclass, every class is implicitly a subclass of Object.

See the Example 8.1.4-1 "Direct Superclasses and Subclasses" in JLS, chapter 8.1.4

It shows that a class Point { int x, y; } "is a direct subclass of Object"

Moreover the documentation says:

Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object.

The JLS states it short and formal:

The subclass relationship is the transitive closure of the direct subclass relationship.

Thus class Object is the superclass of all classes.

But the documentation also says:

Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance).

Going on with the example a class ColoredPoint extends Point { int color; } "is a direct subclass of class Point.". By the transitive relationship it's a (non-direct) subclass of class Object.

Summarizing:
Object is either the direct superclass or by transitive relationship the last superclass of any other class.

Answering the questions:

  • Java does not support multiple inheritance: It provides single inheritence in a transitive way. Every class extends directly only one supercalss.
  • How is the relationship: The class Parent corresponds to the class Point and the class Child to the class ColoredPoint of the JLS example. Only Option 2 shows this relation.
5

Well it is an interesting discussion. I think it will be option no 2. As if you try the below code .

public static void main(String []args){
      Parent p=new Parent();
      Class c= p.getClass();

      Child child =new Child();
      Class c1= child.getClass();
      System.out.println(c.getSuperclass());
      System.out.println(c1.getSuperclass());

 }

You will get output as :

class java.lang.Object 
class Parent
1
  • using the following code solved my doubt: Class child_super = c1.getSuperclass(); System.out.println(child_super.getSuperclass()); Commented Aug 31, 2018 at 9:59
3

Option 2, as every object derives Object.class methods

3

The right answer is Option 2. Any Java class inherit all parents for their parents. In other words.

Class A extends Class B Class B extends Class C Class C extends Class D

Class X extends A -> it means that A inherit all protected/package/public fields from B,C and D.

In your example, Class Child inherit Parent properties but also Object properties in transitive mode.

3

From Class Object

public class Object
Class Object is the root of the class hierarchy.
Every class has Object as a superclass.
All objects, including arrays, implement the methods of this class.

This means that every Java class has Object as root in the hierarchy, not necessarily as its immediate parent.

3

No multiple inheritance means in Java a class extends only 1 class; has one immediate base class. Indirectly a class can have many ancestors: Child has Parent and Object as ancestor "super" classes.

Object --> Parent --> Child
                  --> OtherChild

Relation: 1 --> N

The reason for avoiding multiple inheritance like in C++, was the ambiguity involved:

Pseudo code assuming multiple inheritance:

class A : Comparable
class B : Comparable

class Child : A, B {

    @Override A? B?
    int compareTo(Child rhs) { ... super.compareTo ? ... }
}

A a = new Child();
B b = new Child();
a.compareTo(b);
2
  • 1
    why should we care about which method to override if the signatures are identical? Overriding just implies that we are not satisfied with parents' behaviour and want to implement ours. The problem is, rather, what happens when we call child.compareTo(anotherChild) without overriding (which method are we going to invoke?) Commented Aug 31, 2018 at 10:15
  • 1
    @AndrewTobilko well said; compareTo(a); compareTo(b), compareTo(child); actually I did not want to say anything about multiple inheritance (and interfaces) as that would be out of scope of the question. Just that multiple inheritance is not having an ancestral line; rather that single inheritance means having one (code bearing) parent per class. I also did not treat default methods in interfaces.
    – Joop Eggen
    Commented Aug 31, 2018 at 10:30
3

First of all, using Java 8, it is possible to accomplish Multiple inheritance using Default methods of interfaces.

Secondly, your understanding regarding Object class is correctly represented in 'Option 2'.
However, it is not multiple inheritance, rather multilevel inheritance. 'Option 1' is multiple inheritance.

Please check this link to read more about them.

2

option 2.Object is a superclass of all other classes,but Object may not a dirrect superclass of a classe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.