what is the base class of all classes in java?

The super base class of all the Java classes is the java.lang.Object class. In Java, each Java descends from the Object. An axiom in mathematics is the starting point of reasoning. An axiom can be used to logically derive the other statements. The concept of Object being a Superclass in Java can be understood from it. Being not an explicit requirement forced on the developer, the object class is implicitly extended by the JVM when a class is declared without extending another class. The Object, however, is not extended by the JVM.

The possible reasons behind this design:

  • We can pass around objects without knowing the type using the Object declaration, by having the Object as the superclass of all Java classes.
  • It is only because of the Object class hierarchy that the collection class like ArrayList allows storing any type of class.
  • It is also possible that this design decision was made to bring a common blueprint for all classes and have some list of functions the same among them, i.e, the methods like hashCode(), clone(), toString() and methods for threading which is defined in Object class.

In C++, however, there is not any superclass of all classes. Thus, all object-oriented programming (OOPS) languages need not follow this concept and the reasons placed.

The Superclass of String, Object, and Class:

public class SuperClass {
 
 public static void main(String... args) {
  String str = new String("Hello");
  Class strClass = str.getClass();
  System.out
    .println("The Superclass of String class: " + strClass.getSuperclass());
 
  Object obj = new Object();
  Class objClass = obj.getClass();
  System.out
    .println("The Superclass of Object class: " + objClass.getSuperclass());
 
  Class classClass = objClass.getClass();
  System.out.println("The Superclass of Class class: "
    + classClass.getSuperclass());
 }
}

Output:

The Superclass of String class: class java.lang.Object
The Superclass of Object class: null     
The Superclass of Class class: class java.lang.Object

Explanation:

In the above example, we are displaying the superclass of String class, the superclass of Object class and the superclass of Class class.

Please follow and like us:
Content Protection by DMCA.com