Multiple inheritance in Java, since all classes extend from the Object class?

I have a simple question:

If I declare a class A - means that this class is implicitly inherited from the Object Class.

Now, if class B inherits from class A

Does this class B also not inherit from the class Object?

If so, does this mean that the keyword “extends” somehow overrides implicit inheritance (from the Object class)?

+4
source share
4 answers

Object, , , Java , Object, . , :

public class MyClass extends Object {

, :

public class MyClass {

:

public class MySubClass extends MyClass {

MySubClass MyClass, Object. , : , , - , Java ( to: multiple-inheritance.)

+11

A Object B A. , 3 (2 ). .

`Object`
    |
   `A`
    |
   `B`
+4

, . , A Object, Object A. B A, A, Object. , B Object, A , B.

- :

public class M extends SomeBase, AndAnotherBase {

Java . , , , M SomeBase, AndAnotherBase... Object... M 2 . , , M .

+1

Object, , , B .

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());
     }
 }

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

C . .

Inheritance from is Objectslightly different in that it is implicit. There is no need to talk extends Objectabout any class - it is redundant. But the mechanism of inheritance is no different.

+1
source

All Articles