Doubt about final methods
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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,
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?
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?
RB
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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).
Tony Morris
Java Q&A (FAQ, Trivia)
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
| New rule: no elephants at the chess tournament. Tiny ads are still okay. Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |






