• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Re-Implementation Versus Overriding

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is something straight from the ePractice exam that seemed a bit like a trick question, but let me make sure I've got the idea down right.

In the Heller and Roberts book "Complete Java 2 Study Guide, Fifth Edition" it says that final, when applied to a method, does not allow it to be overridden, so when I came across this question:



I would think that Book would cause a compilation error. However, Page.read () isn't an override, it's a reimplementation. If I removed the 'final' it would then become and override and then it would break. Is that correct?
[ January 25, 2006: Message edited by: Brandon Tom ]
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Brandon Tom:

If I removed the 'final' it would then become and override and then it would break. Is that correct?



Even if you remove the final keyword, the methods are private and private methods are not inherited. So there is no question of overriding them.
Hope I didn't confuse you...
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bcos, the method 'read()'is marked as private final in Book class, the page class method 'read()' probably is reimplementation.

if u try removing private in Book class, Page class will not compile as method 'read()' stands final.

public class Book {
final void read() { System.out.print("book "); }
}
 
ven kaar
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bcos, the method 'read()'is marked as private final in Book class, the page class method 'read()' probably is reimplementation.

if u try removing private in Book class, Page class will not compile as method 'read()' stands final.



anyway , i am not sure whether it is reimplementation(first time i am hearing this)
 
Brandon Tom
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see, so it's the 'private' declaration that is the key here, not the word final. I see, thanks for the input.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic