Junior — Middle
Explain the mechanism of class inheritance from the base class Object in Java and specify whether it occurs explicitly or implicitly.
sobes.tech AI
Answer from AI
In Java, all classes implicitly inherit from the base class Object if not explicitly specified otherwise. This means that when declaring a class without inheritance:
public class MyClass {
// ...
}
The compiler automatically adds inheritance from Object:
public class MyClass extends Object {
// ...
}
The Object class provides basic methods such as equals(), hashCode(), toString(), clone(), and others, which are available to all classes in Java. This ensures a common fundamental functionality for all objects.
Thus, inheritance from Object is implicit for all classes unless specified otherwise.