Sobes.tech
Junior — Middle

Is it possible to specify a universal parent class for all classes in Java and how to define it?

sobes.tech AI

Answer from AI

In Java, the universal parent class for all classes is java.lang.Object. All classes implicitly inherit from this class unless explicitly specified otherwise. This means that any object in Java is an instance of Object.

There is no need to explicitly define a universal parent class — it is already built into the language. If you create a class without specifying extends, it automatically inherits from Object:

public class MyClass {
    // implicitly inherits from Object
}

If desired, you can explicitly specify inheritance:

public class MyClass extends Object {
    // same as above
}

Object provides basic methods such as toString(), equals(), hashCode(), which are available to all classes.