• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Method Override

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.




 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
answer is 2. So what is your question?
 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This problem is described in Effective Java.
 
Ranch Hand
Posts: 71
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 89
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right! The invoked overridden method is resolved to the subclass at runtime.
 
Marshal
Posts: 80954
522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic