Monday, January 6, 2025
HomeProgrammingObject Class in Java

Object Class in Java

The Object class is the root class of all Java classes. Every class in Java directly or indirectly inherits from it.

Key Methods:

  1. toString():
    • Returns a string representation of the object.
    java
    @Override
    public String toString() {
    return "Class Details";
    }
  2. equals(Object obj):
    • Compares two objects for equality.
    java
    obj1.equals(obj2);
  3. hashCode():
    • Returns the hash code of the object.
    java
    int hash = obj.hashCode();
  4. getClass():
    • Returns the runtime class of the object.
    java
    Class cls = obj.getClass();
  5. clone():
    • Creates a copy of the object (requires Cloneable interface).
  6. finalize():
    • Called before garbage collection.
See also  Implementation of a Stack in JavaScript

Example:

java
public class Test {
public static void main(String[] args) {
Object obj = new Object();
System.out.println(obj.toString());
}
}

The Object class ensures consistency and interoperability in Java’s class hierarchy.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x