Hello Muhammad, Actually i couldn't understand your tricky answer, can you please explain this, as i think "pf1.equals(pf2) returns false and is assigned to "Object o", but why equals method of Object class return false? explain this part, as it has made my concept mess up..
You realize that Profile's equals() method is not running--Object's equals() method is running and so, because the two Profile objects
are not the same, false is returned.
You could not legally call Profile's equals() method from another class because it is private. What is curious here is that the jvm chooses
Object's equals method when it sees equals(Profile p) called. It's choices are:
public boolean equals(Object o)
-and-
private Boolean equals(Profile p)
If private Boolean equals(Profile p) had been unambiguous, it would have been an error at compile time to call it from a reference to Profile
object within another class. It seems like you're getting off easy here, as though the compiler/jvm is saying 'we'll overlook this illegal call
to equals(Profile p) because this call could also apply to equals(Object o) which is legal.'
Rajesh you don't need to write the code tags yourself by hand. You are writing [Code] instead of [code]. There is a button to insert code tags in your posts...
lol Rajesh you are asking all examlab questions here..... well Let me tell that this is not a valid override but a overload. Remember contract of a valid override. Access modifiers cannot be restrictive and for overloading the arguments must change so this equals is a overload. Well because its a overload and not a override, the line 1 uses the default equals method(also the class's equals method is not visible to the other class because its private) that is inherited from the Object class thus giving you false. Well in Object class equals method is implemented as a simple (==).
s1.equals(s2) returns true , due to string Constant Pool Initialization. When there is already a " ab " in the heap . Then the next reference though it is a instantiation by new operator. But it points to the same reference in the heap. This is something exceptional in the case of String. I hope this will help you.
The String class overloads equals() to return true if the 'String' stored by the String objects are the same. Your Profile
class, given that the overloaded equals() method is private, still basically uses Object's equals() method which returns
false if the references do not point to the exact same object.
Samrat Som wrote:s1.equals(s2) returns true , due to string Constant Pool Initialization. When there is already a " ab " in the heap . Then the next reference though it is a instantiation by new operator. But it points to the same reference in the heap. This is something exceptional in the case of String. I hope this will help you.
When you initialize String object using new operator, then there is no concept of Pool occur. All objects have different memory in Heap.
Rajesh k Jha wrote:Why 1 results in true and 2 results in false?
Can you please help me in this.
The String class has its own implementation of Equal function which compare the values instead of refrences that's why it returns true.