About Question enthuware.ocajp.i.v7.2.1206 :
Moderator: admin
-
- Posts: 42
- Joined: Mon Jan 20, 2014 2:26 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1206 :
Somewhere deep in an explanation of a different question in a different test,
it was explained, please correct me if i am wrong, that the first thing that the equals() method does is to check if both objects
belong to the same class...(or extending each other. Probably using the instaceof operator....)
it was explained, please correct me if i am wrong, that the first thing that the equals() method does is to check if both objects
belong to the same class...(or extending each other. Probably using the instaceof operator....)
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1206 :
In question 2.1125, the explanation says, "The equals methods of all wrapper classes first check if the two object are of same class or not. If not, they immediately return false", which is correct.
You can see the code here: http://www.docjar.com/html/api/java/lan ... .java.html
If you saw something else, please let me know the question id and I will take a look.
HTH,
Paul.
You can see the code here: http://www.docjar.com/html/api/java/lan ... .java.html
If you saw something else, please let me know the question id and I will take a look.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 63
- Joined: Fri Oct 31, 2014 6:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1206 :
But, in that case, its a violation of contract of the equals method overriding, on such questions cant we assume that, programmer wont violate the contract?So, it is possible that an equals method may return true even if the class of the passed object has no relation to this object.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1206 :
No, it wouldn't necessarily be a violation of the equals contract. The contract says that it needs to be reflexive, symmetric, transient, and consistent. You can satisfy these even with two different classes.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Sun Jun 21, 2015 12:54 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1206 :
I don't quite fully understand the question properly. Would you please explain it?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1206 :
Can you please tell which part do you not understand?
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Sun Jun 21, 2015 12:54 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1206 :
If a.equals(b) returns true, b instanceof ClassOfA must always be true. (Assume that ClassOfA is the name of the class of the variable a.)
Is this how I should interpret the questions as? Please correct me if m wrong
public ClassOfA {
public int a;
}
public ClassOfB extends ClassOfA{
ClassOfA b = new ClassOfB;
if(a.equals(b)){
returns true;
}
}
Is this how I should interpret the questions as? Please correct me if m wrong
public ClassOfA {
public int a;
}
public ClassOfB extends ClassOfA{
ClassOfA b = new ClassOfB;
if(a.equals(b)){
returns true;
}
}
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1206 :
From the statement, the first part tells you that a.equals(b) returns true. While the code that you've given will not even compile. So I am not sure how are you interpreting.
a and b should be variables of some class (not of type int!) such that a.equals(b) return true. You can now think of cases where this holds true and then determine whether b instanceof ClassOfA will also return true or not. ClassOfA is whatever class you choose.
a and b should be variables of some class (not of type int!) such that a.equals(b) return true. You can now think of cases where this holds true and then determine whether b instanceof ClassOfA will also return true or not. ClassOfA is whatever class you choose.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Sun Jun 21, 2015 12:54 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1206 :
I get it now. Thank you. sorry for the bad example.
-
- Posts: 10
- Joined: Wed Aug 08, 2012 5:00 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1206 :
I could not understand the problem, its both return true
class ClassOfA {
}
class ClassOfB extends ClassOfA{
}
public class TestClassAB {
public static void main(String[] args) {
ClassOfA a = new ClassOfA();
ClassOfA b =a;
if(a.equals(b)){
System.out.println("true");
System.out.println(b instanceof ClassOfA);
}
}
}
class ClassOfA {
}
class ClassOfB extends ClassOfA{
}
public class TestClassAB {
public static void main(String[] args) {
ClassOfA a = new ClassOfA();
ClassOfA b =a;
if(a.equals(b)){
System.out.println("true");
System.out.println(b instanceof ClassOfA);
}
}
}
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1206 :
You are using the same object in your code! Please read the question carefully.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 59 guests