0% found this document useful (0 votes)
66 views17 pages

Topics: Object Class in Java

- The Object class is the supermost class in Java and all classes directly or indirectly inherit from it. - The Object class contains important methods like hashCode(), equals(), toString() that are used to check object equality, generate hashcodes, and convert objects to strings. - When a method is called on an object, Java first searches for the method in the current class and then searches up the inheritance hierarchy to find the method, starting from the direct superclass and ultimately reaching the Object class.

Uploaded by

Shubhankar Singh
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)
66 views17 pages

Topics: Object Class in Java

- The Object class is the supermost class in Java and all classes directly or indirectly inherit from it. - The Object class contains important methods like hashCode(), equals(), toString() that are used to check object equality, generate hashcodes, and convert objects to strings. - When a method is called on an object, Java first searches for the method in the current class and then searches up the inheritance hierarchy to find the method, starting from the direct superclass and ultimately reaching the Object class.

Uploaded by

Shubhankar Singh
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/ 17

Topics

• Object class in Java

1 Object-Oriented Programming Using Java


Object class in Java

• Supermost class in Java. [java.lang.Object]


• If a class does not extend any super class then that
class is a direct sub class of Object class.

class X class Y class Z


{ { {
}// End of class X }// End of class Y }// End of class Z

• Classes X, Y and Z as shown above are the direct sub-


classes of Object class.
• Every class in Java, directly or in-directly is a sub-class
of Object.

2 Object-Oriented Programming Using Java


Important Methods of Object
class
•public int hashCode()

•public boolean equals(Object obj) Discussed in this


Lecture
•public String toString()

•protected void finalize() throws Throwable

3 Object-Oriented Programming Using Java


How Java Searches For Called
Methods
Super class of A is Object
class A Not Found in A
Last Search in Object class
{ A
….. Not Found in Object
}// End of class A Not Found in B
class B extends A Compile-Time Error
{ B
….. D d1 = new D();
}// End of class B Not Found in C
class C extends B d1.someMethod(….);
{
C
…..
}// End of class C Not Found in D
class D extends C First Search in D
{
….. D
}// End of class D

4 Object-Oriented Programming Using Java


public int hashCode()

• Returns the hash-code of the Object. Hashcode  an


Integer Representation of an Object.
• hashCode() Method of Object class considers the
memory address of the object as the hash-code
• So, the method returns the memory address of the
object in hexadecimal form as the hash-code value of
that object

5 Object-Oriented Programming Using Java


public int hashCode() : Example
// File Name : Test.java
class A { } // End of class A
hashCode() Method
class Test
Invoked from Object
{
class
public static void main(String args[])
{
A a1 = new A();
A a2 = new A();
A a3 = a1;

System.out.println("Hash Code a1 :" + a1.hashCode());


System.out.println("Hash Code a2 :" + a2.hashCode());
System.out.println("Hash Code a3 :" + a3.hashCode());
<<OUTPUT>>
}// End of Method F:\>java Test
}// End of class Test Hash Code a1 :1284693
Hash Code a2 :31168322
Hash Code a3 :1284693
6 Object-Oriented Programming Using Java
public booelan equals()

• Compares this object-reference and ‘obj’ for equality and


returns true if equal otherwise false
• The equals() method of Object class tests whether the
hash-codes of the object are equal or not.
• Hash-codes (as per the implementation hashCode()
Method in Object class) of two objects are equal if and
only if they have same memory address

7 Object-Oriented Programming Using Java


equals() Method : Example
:A
// File Name : Test.java a1
class A { } // End of class A
class Test a3
{ :A
public static void main(String args[]) a2
{
A a1 = new A();
A a2 = new A();
A a3 = a1;
<<OUTPUT>>
F:\>java Test
false
System.out.println(a1.equals(a2));
System.out.println(a1.equals(a3));
true

}// End of Method equals() Method is called from


}// End of Test class
Object class

8 Object-Oriented Programming Using Java


== (Equality Operator) for
Object-Reference Equality : Example
:A
// File Name : Test.java
class A { } // End of class A a1
class Test
{ a3
public static void main(String args[]) :A
{
A a1 = new A(); a2
A a2 = new A();
A a3 = a1;
if(a1 == a2)
System.out.println(“Hello”); <<OUTPUT>>
else
System.out.println(“Hi”); F:\>java Test
if(a1 == a3) Hi
System.out.println(“Thanks”);
else Thanks
System.out.println(“Welcome”);
}// End of Method
}// End of Test class ‘==‘ equality operator when used for object-references returns
true only if object-references points to the same object
9 Object-Oriented Programming Using Java
Supplying equals() Method in
class
• You can overload the equals() Method as follows
public boolean equals(<T> obj)
{
<T> is class type in which
….. the method is supplied
} // End of Method
• You can override the equals() Method as follows
public boolean equals(Object obj)
{
T var = (T) obj;
…..
Parameter is Object Type
} // End of Method
Type cast the parameter first to local class and then code equals Method
10 Object-Oriented Programming Using Java
Supplying equals() Method in
a class: Example
// File Name : Test.java Circle c1 = new Circle(10.5);
class Circle
{
private double radius; // Instance Field : radius Circle c2 = new Circle(6);
// Constructor Method
Circle(double radius) { this.radius = radius; }
// Accessor Method Circle c3 = new Circle(10.5);
public double getRadius() { return this.radius; }
// Method to compute area
public double area() { return 3.1456 * radius * radius; } System.out.println(c1.equals(c2));
// Method to compute perimeter
public double perimeter() { return 2* 3.1456 * radius; }
System.out.println(c1.equals(c3));
// equals method  Method Overriding
public boolean equals(Object o)
{
Circle c = (Circle) o; // First Type Cast
return this.area() == c.area(); equals() Method in this case
} // End of method will be invoked from Circle
} // End of class Circle
<<OUTPUT>> class
F:\>java Test
false
true
11 Object-Oriented Programming Using Java
Supplying equals() Method in
a class: Example
// File Name : Test.java Circle c1 = new Circle(10.5);
class Circle
{
private double radius; // Instance Field : radius Circle c2 = new Circle(6);
// Constructor Method
Circle(double radius) { this.radius = radius; }
// Accessor Method Circle c3 = new Circle(10.5);
public double getRadius() { return this.radius; }
// Method to compute area
public double area() { return 3.1456 * radius * radius; } System.out.println(c1.equals(c2));
// Method to compute perimeter
public double perimeter() { return 2* 3.1456 * radius; }
System.out.println(c1.equals(c3));
// equals method  Method Overloading
public boolean equals(Circle o)
{
return this.area() == c.area();
} // End of Method equals() Method in this case
} // End of class Circle will be invoked from Circle
class

12 Object-Oriented Programming Using Java


public String toString() Method

• public String toString()


 Returns string form of Object
 System.out.println()  Always displays in String form
 The default toString() method in Object class displays the
output in following form
<<class-name-of-Object> @ <<hash-code-of-object>>
 System.out.println()  calls toString() upon the parameters
that belongs to class type. For Example

System.out.println(x);  System.out.println(x.toString());

13 Object-Oriented Programming Using Java


public String toString() Method
: Example
// File Name : Test.java
class Circle
Circle c1 = new Circle(10.5);
{
private double radius; // Instance Field : radius
// Constructor Method
Circle c2 = new Circle(6);
Circle(double radius) { this.radius = radius; }
// Accessor Method
public double getRadius() { return this.radius; }
Circle c3 = new Circle(10.5);
// Method to compute area
public double area() { return 3.1456 * radius * radius; }
// Method to compute perimeter
System.out.println(c1);
public double perimeter() { return 2* 3.1456 * radius; } System.out.println(c2);
} // End of class Circle
System.out.println(c3);

• Circle class has not supplied any


System.out.println(c1.toString());
toString() Method System.out.println(c2.toString());
• In this Example toString() will be called System.out.println(c3.toString());
from Object class <<OUTPUT>>
Circle@139a55
Circle@1db9742
Circle@106d69c
14 Object-Oriented Programming Using Java
Supplying a toString() Method
in a class : Example 1
// File Name : Test.java
class Circle
Circle c1 = new Circle(10.5);
{
private double radius; // Instance Field : radius
// Constructor Method
Circle c2 = new Circle(6);
Circle(double radius) { this.radius = radius; }
// Accessor Method
public double getRadius() { return this.radius; }
Circle c3 = new Circle(10.5);
// Method to compute area
public double area() { return 3.1456 * radius * radius; }
// Method to compute perimeter
System.out.println(c1);
public double perimeter() { return 2* 3.1456 * radius; } System.out.println(c2);
// Supplying toString() Method
System.out.println(c3);
public String toString()
{
return “Welcome to Object World”);
}// End of Method

} // End of class Circle

• Circle class has supplied its own <<OUTPUT>>


Welcome to Object World
toString() Method
Welcome to Object World
• In this Example toString() will be called Welcome to Object World
from class
15 Object-Oriented Programming Using Java
Supplying a toString() Method
in a class : Example 2
// File Name : Test.java
class Circle
Circle c1 = new Circle(10.5);
{
private double radius; // Instance Field : radius
// Constructor Method
Circle c2 = new Circle(6);
Circle(double radius) { this.radius = radius; }
// Accessor Method
public double getRadius() { return this.radius; }
Circle c3 = new Circle(10.5);
// Method to compute area
public double area() { return 3.1456 * radius * radius; }
// Method to compute perimeter
System.out.println(c1);
public double perimeter() { return 2* 3.1456 * radius; } System.out.println(c2);
// Supplying toString() Method
System.out.println(c3);
public String toString()
{
return “Radius: “ + this.radius + ” Area=“ + this.area() ;
}// End of Method

} // End of class Circle

• Circle class has supplied its own <<OUTPUT>>


toString() Method Radius: 10.5 Area=346.8024
Radius: 6.0 Area=113.2416
• In this Example toString() will be called Radius: 10.5 Area=346.8024
from class
16 Object-Oriented Programming Using Java
Thank You

17 Object-Oriented Programming Using Java

You might also like