-6

Create the hashCode and equals method for the following class.

private static class MyOb {

    private String name;
    private Integer quality;
    private final int MAXIMUM = 23;

}

I could not solve this question

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
prem
  • 7
  • 1

2 Answers2

2

Java class has default hashCode and equals method method implemented through super class. If u want to over ride them u can by following:

class MyOb {

private String name;
private Integer quality;
private final int MAXIMUM = 23;

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    result = prime * result + ((quality == null) ? 0 : quality.hashCode());
    return result;
}
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    MyOb other = (MyOb) obj;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    if (quality == null) {
        if (other.quality != null)
            return false;
    } else if (!quality.equals(other.quality))
        return false;
    return true;
}

}

Note: class cannot be private or static

Naresh kumar
  • 1,069
  • 1
  • 7
  • 21
-1

if you want see hash code of class. create object

    Myob myObject=new Myob();
    System.out.println(myObject);

if want to equals method

    myObject.equals(which one you want to campare);
subhakar patnala
  • 319
  • 1
  • 4
  • 15