5

I have a simple question:

If I declare a class A - means this class in implicitly inheriting from Object Class.

Now If class B inherits from class A

is this Class B also not Inheriting from Object class ?

If yes, does that mean writing the keyword 'extends' some how override the implicit inheritance ( from class Object) ?

GV1234
  • 73
  • 3
  • 7

4 Answers4

12

All classes extend form Object, either implicitly or explicitly, directly or indirectly, the whole class hierarchy in Java ends up pointing at Object, which is at the root. For instance, when you write this:

public class MyClass extends Object {

Is exactly the same as this:

public class MyClass {

And if we have this:

public class MySubClass extends MyClass {

Then MySubClass extends from MyClass which extends from Object. It's a transitive inheritance relationship, and it occurs in only one direction: at no point in the hierarchy it will be possible that a single class extends from more than one class - that's why we say that in Java we have single-inheritance (as opposed to: multiple-inheritance.)

Óscar López
  • 232,561
  • 37
  • 312
  • 386
4

A inherits from Object and B inherits from A. So you have 3 levels of inheritance (2 explicit). There is no multiple inheritance here.

`Object`
    |
   `A`
    |
   `B`
Adam Arold
  • 29,285
  • 22
  • 112
  • 207
1

When you 'extends', your class gets everything from the base class and adds extra stuff. So, if A extends Object, you've got all of Object in A. If B extends A, it gets all of A which also has all of Object. So, B now has Object, A and anything in B itself.

Multiple inheritence is when you extend a class from many things at once:

public class M extends SomeBase, AndAnotherBase {

This isn't allowed in Java. It adds complication in languages that do allow it because you can end up with class M being comprised of SomeBase, AndAnotherBase... and if they both already derive from Object... M would have 2 Objects in it. This starts to get tricky to deal with as you have to know which Object within M you're dealing with.

Scott Langham
  • 58,735
  • 39
  • 131
  • 204
1

You specifically mention inheriting from Object, but if you are asking about inheritance in general between children and grandchildren, then yes, B will inherit from all predecessors.

public class A{
   public String getAString(){
       return "Hello from A";
   }
 }


public class B extends A {
    public String getBString(){
        return  getAString() + " and Hello from B.";
    }
}


 public class C extends B{
 }

 public class Main{
     public static void main(String[] args){
         C c = new C();
         //                 Inherited from A         Inherited from B
         System.out.println(c.getAString() + "..." + c.getBString());
     }
 }

This gives

Hello from A...Hello from A and Hello from B.

Notice C has no code in the class body. All functionality is being inherited.

Inheriting from Object is a little special in that it is implicit. There is no need to say extends Object on any class -- it's redundant. But the inheritance mechanism is no different.

MadConan
  • 3,749
  • 1
  • 16
  • 27