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

Object Class

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Object Class

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Object class

Object class :
● Object class is defined in java.lang package.
● Object class is a supermost parent class for all the classes in java.
● One no argument constructor is there.

In object class there are 11 non static methods.


List of 11 methods ➔
public String toString()

public boolean equals(Object o)

public int hashCode()

protected Object clone() throws CloneNotSupportedException

protected void finalize()

final public void wait() throws InterruptedException

final public void wait(long l) throws InterruptedException

final public void wait(long l , int i) throws InterruptedException

final public void notify() throws InterruptedException

final public void notifyAll() throws InterruptedException

final public void getClass()


toString()
● toString() method returns String.
● toString() implementation of Object class returns the reference of an
object in the String format.
Return Format : ClassName@HexaDecimal
EXAMPLE :
class Demo
{
public static void main(String[] args)
{
Demo d = new Demo();
System.out.println(d) // d.toString() --- Demo@2f92e0f4
}
}
NOTE :
● Java doesn’t provide the real address of an object.
● Whenever programmer tries to print the reference variable toString() is
implicitly called.

PURPOSE OF OVERRIDING toString() :


We override toString() method to return state of an object instead
of returning reference of an object.
EXAMPLE :
class Circle
{
Int radius ;
Circle(int radius)
{
this.radius = radius ;
}
@Override
public String toString()
{
return “radius : ”+radius ;
}
public static void main(String[] args)
{
Circle c = new Circle(5);
System.out.println(c) // c.toString()---radius : 5
}
}
equals(Object) :
● The return type of equals(Object) method is boolean.
● To equals(Object) method we can pass reference of any object.
● The java.lang.Object class implementation of equals(Object)
method is used to compare the reference of two objects.

Program : Refer Drive link for Object class Program


NOTE :
● If the reference is same == operator will return true else it returns
false.
● The equals(Object ) method is similar to == operator.

PURPOSE OF OVERRIDING equals(Object) :


We can override to equals(Object) method to compare the state
of an two Objects instead of comparing reference of two Objects.

NOTE :
● If equals(Object ) method is not overridden it compares the reference of two
objects similar to == operator.
● If equals(Object) method is overridden it compares the state of two objects,
in such case comparing the reference of two objects is possible only by ==
operator.
Design tip :
In equals method compare the state of an current(this) object with the passed
object by downcasting the passed object.

➔ Refer Student Program in Object class Folder


hashCode() :
● The return type of hashCode() method is int.
● The java.lang.Object implementation of hashCode() method is used to
give the unique integer number for every object created.
● The unique number generated based on the reference of an object.

PURPOSE OF OVERRIDING hashCode() :


If the equals(Object) method is overridden , then it is necessary to override
the hashCode() method.
Design tip :
hashCode() method should return an integer number based on the state of an
object.
EXAMPLE 1:
class Pen
{
double price ;
Pen(double price)
{
this.price = price ;
}
@Override
public int hashCode()
{
int hc = (int)price;
return hc ;
}
}

You might also like