Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes que on equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "que on equals()" Watch "que on equals()" New topic

que on equals()

nisha chidella
Ranch Hand

Joined: Jul 24, 2008
Posts: 57

Question 2 : which statement are true?

class Employee
int empid;
String name;
Employee(int id,String name)

public int getEmpId()
return empid;

public String getString()
return name;

public boolean equals(Employee o)
if ((o instanceof Employee) && (((Employee)o).getEmpId() == this.empid))
return true;
return false;

class JavaBeat2
public static void main(String[] args)
Employee x1 = new Employee(1,"abc");
Employee x2 = new Employee(2,"def");
Employee x3 = new Employee(1,"abc");
if( x1.equals(x3) )
System.out.println("x1 and x3 are equal");
System.out.println("x1 and x3 are not equal");


1. compile Time error.
2. Runtime Error.
3. x1 and x3 are equal.
4. x1 and x3 are not equal.
5. equals method in class Employee is legal overridding of equals method in class Object

The ans given is (2).
why ? the override is legal and so, shudn't the ans be (3) and (4)

SCJP 5.0(100%)
Preparing for SCWCD
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Override is not legal. It is taking Objevt of employee instead of Object.

This will be the correct overriding of equals.

My Blog SCJP 5 SCWCD 5
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Override is not legal.
But answer should be
3. x1 and x3 are equal.

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
i compiled the above program then i got ouput as

x1 and x3 are equal..

SCJP5 and SCWCD1.5
Think Twice Act Wise...
Ankit Garg

Joined: Aug 03, 2008
Posts: 9466

Override is not legal.

Just a slight correction if you would like to say it, the equals method is never overridden. The equals method has been overloaded legally in this code ...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Geraldo Maciel

Joined: Oct 17, 2008
Posts: 2
indeed the equals method is not overridden but overloaded int this code.

the correct answer should be number 3, since the overloading method (the one that takes the Employee argument) is the onde called in the main method of class JavaBeat2.
I agree. Here's the link:
subject: que on equals()
It's not a secret anymore!