• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question on equals & instanceof

 
thejaka samarakoon
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the result of compiling the following class?


Answers
A. The class compiles successfully.
B. Line 6 causes a compiler error because hashCode does not return a unique value.
C. Line 10 causes a compiler error because the equals method does not override the parent
method correctly.
D. Line 14 does not compile because the ClassCastException is not handled or declared.
E. Line 15 does not compile because other.ISBN is a private field.


Question 1
Why A is the answer?

Question 2
What is this.ISBN refers to?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thejaka samarakoon wrote:

Question 1
Why A is the answer?

Question 2
What is this.ISBN refers to?


There is no problem, so why do you think, there might be a error? this mean, the object, on which you've invoked the equals() method.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thejaka samarakoon wrote:
Question 1
Why A is the answer?

what makes you to think that this program wont compile?

thejaka samarakoon wrote:
Question 2
What is this.ISBN refers to?


it holds int values.[default 0]
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a side note : all the instances of this class would be same always[in case of equals check]
 
thejaka samarakoon
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:As a side note : all the instances of this class would be same always[in case of equals check]


Is it in this example, this.ISBN is referring to Book object ?That is the thing i have to be clear with.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this.ISBN is same as ISBM . i.e, currently executing object.

related to my previous post, how do you set value to ISBN?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thejaka samarakoon wrote:
Is it in this example, this.ISBN is referring to Book object ?That is the thing i have to be clear with.


this means, the currently executing object, and this.ISBN means the variable of the currently executing object!
 
thejaka samarakoon
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Abimaran Kugathasan & Seetharaman Venkatasamy
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thejaka samarakoon wrote:Thanks Abimaran Kugathasan & Seetharaman Venkatasamy


You are welcome
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thejaka samarakoon wrote:Thanks Abimaran Kugathasan & Seetharaman Venkatasamy

You are Welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic