• 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

Doubt about final methods

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading the book "Programmer's guide to Java Certfication" by Mr Mughal.. 2nd edition. In the chapter about object oriented programming section 6.2 p235 talking about final methods,


A final method cannot be overridden because the modifier final prevents method overriding. An attempt to override a final method will result in a compile-time error. However, an abstract method requires the non-abstract subclasses to override the method, in order to provide an implementation.


Hmm.. i thought about the last sentence. What i understood is that you can over ride an abstract "final" method in order to provide an implementation.

which prompted to define a final method in an abstract class. But it would not compile.



Was that a mis-understanding on my part?
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The abstract and final modifiers are mutually exclusive when applied to methods. That is, you can have one, the other, neither, but not both (or a compile-time error results).
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you have no doubts on final method cannot override. According to Mughal, an abstract method in superclass must override in non abstract sub-class with implementation. If sub-class override without implementation then sub-class is abstract too. Take a look on followin code:

abstract class a //must be abstract coz has abstract method.
{
abstract void x(); //abstract method
}

class b extends a
//non abstract class so it must provide implementation for overriding
// method "x" in superclass "A"
{
void x() //overriding method "x" from superclass "a"
{
//with implementation
}
}

But the sub-class is abstract or non-abstract it can provide implementation for overriding method but what Mughal points out there is shown in above snippets.

Hope you understand.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic