Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

que on equals()

 
nisha chidella
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
link: http://www.javabeat.net/javabeat/scjp5/mocks/scjp-5-0-questions-1.php

Question 2 : which statement are true?


class Employee
{
int empid;
String name;
Employee(int id,String name)
{
this.empid=id;
this.name=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;
}
else
{
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");
}
else
{
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)
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Override is not legal. It is taking Objevt of employee instead of Object.

This will be the correct overriding of equals.

 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Override is not legal.
But answer should be
3. x1 and x3 are equal.
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i compiled the above program then i got ouput as

x1 and x3 are equal..
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
Geraldo Maciel
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic