Object Class Functions

Object Class Functions

1) equals
2) toString
3) hashCode

class Cube
{

int h;

Cube()
{
h=3;
}

Cube(int w)
{
this.h=w;
}

}

class Rect extends Cube
{

int l,b;

Rect()
{
l=0;
b=0;
}

Rect(int i)
{
super(i);
this.l=i;
this.b=i;
}

Rect(int i, int j)
{
super(i);
this.l=i;
this.b=j;
}

Rect(int i, int j,int k)
{
super(k);
this.l=i;
this.b=j;
}

//toString() Overloading
public String toString()
{
 return " Length : "+l+" Breath : "+b;
}

//equals() overloading
public boolean equals (Object o)
{
return ((this.l * this.b)==(((Rect)o).l * ((Rect)o).b));
}

public static void main(String s[])
{
Rect r1 = new Rect(6);
Rect r2 = new Rect(12,3,6);
Rect r3 = new Rect(9,4);
Rect r4 = new Rect(6);

System.out.println(r1.equals(r2));
System.out.println(r2.equals(r3));
System.out.println(r3.equals(r1));
System.out.println(r1);
System.out.println(r2+ " " +r3);

//How to Use hashCode() Function
System.out.println(r1.hashCode());
System.out.println(r4.hashCode());
        
}
}

No comments:

Post a Comment