//Student.java
class Student{ private int roll; private String name; public Student(int roll,String name){ this.roll=roll; this.name=name; } public int hashCode(){ return roll+name.length(); } public boolean equals(Object obj){ Student s=(Student)obj; return (this.roll==s.roll && this.name.equals(s.name)); } }
//IssueID.java
class IssueID{ public static void issueID(Student s1,Student s2){ if(s1.equals(s2)) System.out.println("New ID issued"); else System.out.println("New ID NOT issued"); } }
//Institute.java
import java.lang.Object; class Institute{ public static void main(String[] args){ Student s1=new Student(38,"shiva"); Student s2=new Student(45,"aditya"); IssueID.issueID(s1,s2); System.out.println(s1.hashCode()); System.out.println(s2.hashCode()); } }
As in the code above, I overridden the hashCode() method. This may sound silly, but can I access the java.lang.Object.hashCode() method using the same Student objects (s1 and s2) at the same time?
source share