
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why Object Class is the Super Class for All Classes in Java
The java.lang.Object class is the root or superclass of the class hierarchy, it belongs to the java.lang package. All predefined classes and user-defined classes are direct or indirect subclasses of the Object class.
Why is Object Class a Superclass?
To understand why the Object class is the superclass of every class in Java, we need to understand why a superclass is needed in the first place:
- To share common properties.
- To accept and return any type of object.
Sharing Common Properties using Object Class
There are 11 common properties that every Java class must implement. But it would be difficult for developers to repeat the same boilerplate code in each class. The Object class implements all these 11 properties through 11 methods, making Java programming a little simpler,
All these methods have a generic logic common to all the subclasses, if this logic does not satisfy specific requirements of the subclass, then the subclass can override it.
Example
The toString() method is of the Object class. By default, it returns a string representation of the object's memory address. In the following Java program, we are overriding it to print the name property of the class.
class JavaDev { String name; // constructor JavaDev(String name) { this.name = name; } // overriding toString method of Object class @Override public String toString() { return "Developer Name: {'" + name + "'}"; } } public class Team { public static void main(String[] args) { JavaDev developer = new JavaDev("Shriansh"); System.out.println(developer.toString()); } }
When you run the above code, it will print the following result:
Developer Name: {'Shriansh'}
Accepting and returning any type of Object
A single method can receive and send any type of class object as an argument or return type. It is possible due to a superclass, which is the Object class.
Example
Let's see an example where a method will accept an object as an argument and return it:
class ObjectCls { // object type argument public Object returnObj(Object obj) { System.out.println(obj.toString()); // return object return obj; } } public class Main { public static void main(String[] args) { ObjectCls obj1 = new ObjectCls(); String str = "This is String Object"; Integer num = 42; obj1.returnObj(str); obj1.returnObj(num); } }
When you run the code, it will return and print the passed object:
This is String Object 42
Common Functionalities of Every Class Object
A list of properties that are implemented by every class in Java is given below. We need to remember that these properties are represented by methods and are defined inside the Object class.
-
Comparing two objects: The boolean equals(Object obj) method is used to compare two objects. It returns TRUE if specified objects represent the same value.
-
Retrieving hashcode: The public int hashcode() method returns the hash code value of the specified object.
-
Retrieving object reference: To get the run-time class object reference, we use the final Class getClass() method.
-
Retrieving object information: The String toString() method is used to display object information in String format.
-
Object clean-up: The void finalize() method cleans up the Object when there is no reference available.
-
Waiting for current thread: To wait for the current thread until another thread invokes the notify(), we use the public final void wait() method, and if you want to wait till a specified time, use the public final void wait(long timeout, int nano).
-
Notify about object lock availability: For a single thread, we use public final void notify(). For multiple threads, we use public final void notifyAll().