-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverloadingEqualsMethodOfObjectClass.java
More file actions
36 lines (29 loc) · 1.17 KB
/
OverloadingEqualsMethodOfObjectClass.java
File metadata and controls
36 lines (29 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Circle1{
float radius;
Circle1(float radius){
this.radius = radius;
}
public double area(){
return 3.14*radius*radius;
}
// Here we are going to overload the equals method of Object class
public boolean eeuals(Circle1 circle1){
if (this.area()==circle1.area()){
return true;
}else
return false;
}
}
public class OverloadingEqualsMethodOfObjectClass {
public static void main(String[] args) {
Circle1 cir1 = new Circle1(3);
Circle1 cir2 = new Circle1(4);
Circle1 cir3 = new Circle1(3);
System.out.println(cir1.eeuals(cir3));
// Although these objects are pointing to different memory location but this equals comparision returns
//true because the equals method is overridden and now it checks for area
System.out.println(cir2.toString()); // Prints name_of_the_class@hashcodeOfTheObject --> Circle1@1b6d3586
//or we can try simply printing object it will call toString method of the object class implicitly
System.out.println(cir2); // Prints name_of_the_class@hashcodeOfTheObject --> Circle1@1b6d3586
}
}