• Post Reply Bookmark Topic Watch Topic
  • New Topic

Polymorfism. Dynamic binding. Invoking wrong method.  RSS feed

 
Sven Sylta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is the equals-method in the super-class invoked? Shouldn't the equals-method in the sub-class be invoked(at least in the first if-statement since b2 is a B(i know B is also an A))? Thanks for your help.

Edit: Is the equals-method overridden? Or does B have its own equals-method?






 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sven Sylta wrote:Is the equals-method overridden? Or does B have its own equals-method?

No, the "equals" method in "B" is not overriding the one in class "A". The best way to find out is not by asking, but by using "@Override" annotation over the method you are intending to override. The compiler will tell you whether it's a valid override or not.

For more on method overriding, and rules governing, whether a method is overriding a superclass method or not, refer to JLS Section 8.4.8.1 - Overriding. Kindly go through it.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sven Sylta wrote: . . . Edit: Is the equals-method overridden? Or does B have its own equals-method? . . .
No, as you have been told, it is not overridden; it is incorrectly implemented. If you add the annotation as R Jain told you, it will fail to compile; the parameter should be of type Object. Also, you ought to override hashCode().
That equals method will result in incorrect results like this:-I think your method would work correctly given the correct parameter type. You have some poor style however; you should have lots more {} and your last statement should read return (aVal == other.aVal);

It prints a1 equals b2, which rather surprises me. What happens if you try b2.equals(a1)? It ought to give the same result. I shall leave it to you to try and work out why.
I think what happens is that you have overloaded the method, so the A version is called on the a1 reference, even though it points to b1; of course the two results of getClass() are the same.
On the a1 reference, you are passing two A references which actually have different Class objects, so getClass() == a.getClass() returns false.
 
Sven Sylta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should have mentioned that this is just an old exam-question so it's only supposed to be tricky.

Why is it considered to be bad style not to use {}, practical reasons in case you need more code? In my opinion it's easier to read without them. Just curious.

You were right about b2.equals(a1) returning the same value.

Thanks for the answers.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The missing {} make it easy to add additional code and “break” the if statements.… and you don't realise that you have moved i++; out of the if statement. You thought you had added the print statement without changing anything else, but you have changed the entire logic of the program and probably introduced an error. If you had used {} that error would never have happened. More details here and you see Sun had the same convention. The only difference between those two conventions is about the location of the opening {.

You said “Dynamic binding” in the thread title, but there is no dynamic binding. Any binding in this example is static, possibly better called compile‑time binding.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!