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)?
Object, , , Java , Object, . , :
Object
public class MyClass extends Object {
, :
public class MyClass {
:
public class MySubClass extends MyClass {
MySubClass MyClass, Object. , : , , - , Java ( to: multiple-inheritance.)
MySubClass
MyClass
A Object B A. , 3 (2 ). .
A
B
`Object` | `A` | `B`
, . , 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 .
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 . .
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.
extends Object