0% found this document useful (0 votes)
2 views

Object Class

The Object class in Java is the universal superclass from which all classes derive, providing essential methods like toString(), hashCode(), equals(), getClass(), and finalize(). These methods facilitate object representation, comparison, and memory management. The document also includes an example of a Test class demonstrating the use of these methods.

Uploaded by

Gojo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Object Class

The Object class in Java is the universal superclass from which all classes derive, providing essential methods like toString(), hashCode(), equals(), getClass(), and finalize(). These methods facilitate object representation, comparison, and memory management. The document also includes an example of a Test class demonstrating the use of these methods.

Uploaded by

Gojo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Object Class

Universal Super Class—Object Class


• Object class is a special class and it is at the top of the class hierarchy tree.
• It is the parent class or super class of all in Java.
• Hence, it is called Universal super class.
• Object is at the root of the tree and every other class can be directly or
indirectly derived from the Object class.
• The Object class is beneficial if you want to refer any object whose type you
don't know.
• Note that parent class reference variable can refer the child class object, know
as upcasting.
• Let's take an example, there is getObject() method that returns an object but it
can be of any type like Employee,Student etc, we can use Object class
reference to refer that object. For example:
Object obj=getObject();//we don't know what object will be returned from this method
Object Class
Object Class
toString() method
• The toString() provides a String representation of an object and is used to convert an
object to a String.
• Default behavior of toString() is to print class name, then @, then unsigned
hexadecimal representation of the hash code of the object
hashCode() method
• For every object, JVM generates a unique number which is a hashcode.
• It returns distinct integers for distinct objects.
• It returns a hash value that is used to search objects in a collection.
• The main advantage of saving objects based on hash code is that searching becomes
easy.
• Override of hashCode() method needs to be done such that for every object we generate
a unique number. For example, for a Student class, we can return the roll no. of a student
from the hashCode() method as it is unique.
Object Class
equals(Object obj) method
• It compares the given object to “this” object (the object on which the method is
called).
• It gives a generic way to compare objects for equality.
• It is recommended to override the equals(Object obj) method to get our own
equality condition on Objects.
getClass() method
• It returns the class object of “this” object and is used to get the actual runtime
class of the object.
finalize() method
• This method is called just before an object is garbage collected.
• We should override finalize() method to dispose of system resources, perform
clean-up activities and minimize memory leaks.
Object Class
public class Test {
public static void main(String[] args){
Test t = new Test();
System.out.println(t.hashCode());
// Below two statements are equivalent
System.out.println(t);
System.out.println(t.toString());
t = null;
System.gc(); // calling garbage collector
System.out.println("end");
}
@Override public int hashCode() { return 100; }
@Override protected void finalize()
{
System.out.println("finalize method called");
}
}

You might also like