Cosmic Superclass in JavaIn Java, the Object class is the parent class of all the Java classes. Every Java class is a direct or indirect child of the Java Object class. Hence, every Java class extends the Object class. Therefore, we need not to write the following statement to inherit the class. The subclass internally inherits all the methods of the Object class. Hence, we can say that the Object class is the cosmic superclass in Java. The class belongs to java.lang package. Java Object Class: The Cosmic Superclass
The Object class can be used if we want to refer to any object whose type is unknown. Note that the parent class reference variable can refer to the child class object, the mechanism is known as upcasting. The Object class provides methods for some common behaviors to all the objects such as comparing two objects, create a clone of an object, the object can be notified, etc. The four most important methods of the cosmic superclass (Object class) are as follows:
Let's see an example. ObjectComparison.java Output: false false true In the above program, observe that we have not extended the Java Object class but used the equals() method of the Object class. Let's see another example. ToStringExample.java Output: The sum of integers is: The sum is: 150 ToStringExample@490d6c15 In the above example also, we have used the toString() method of the Object class without extending the class. Therefore, the Java Object class is known as a cosmic superclass.
Next TopicShallow Copy Java
|