• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method Override  RSS feed

 
Jano Frank
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many senior developers would miss in their code that the count will not be 1 when main executes? I missed this, and I do not feel well about it.




 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
answer is 2. So what is your question?
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This problem is described in Effective Java.
 
Bill foster
Ranch Hand
Posts: 66
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, you got to be careful on how you over riding methods and using super keyword. Here a link in the tutorial on OracleSuper key word.

You might try to use a debugger when trying to figure out code (after trying to trace it yourself), I use that a lot, because it helps me step through the program.

Also you can use system.out.println() to see how the methods are working( this is shown in the link above).


Hope this helps!


Bill
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:This problem is described in Effective Java.

To further elaborate on this, using a method to invoke an overridable method is not a problem. In fact, it is a good example of polymorphism. However, Effective Java (Item 17) advises that ample documentation must be provided for "self-use of overridable methods".

On the other hand, using a constructor to invoke an overridable method is a problem because this can cause bugs in your program. Effective Java (Item 17) warns against this.
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I think is happening. Correct me if I'm wrong:

When you call tst.addAll(); count is incremented by 1 and control goes to the super class version of addAll() as is specified by super.addAll(); but inside the addAll() method of the superclass when you call the add() method it actually calls the overridden add() method in the subclass because the object through which we called it was a subtype.

Am I right? :|
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right! The invoked overridden method is resolved to the subclass at runtime.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe more accurate to say that the runtime type of the object is used to determine which version of the instance method to use.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!