SCJP2. Please Indent your code using UBB Code
Originally posted by Jose Botella:
From the same place in the JLS where you quoted:
"A method can be declared final to prevent subclasses from overriding or hiding it. It is a compile-time error to attempt to override or hide a final method."
Thus the point in a final method is that can't be changed by any subclasses: It is the last version of it, it doesn't need any more overriding (refinement) in the subclasses. You can't replace it by other via hidding or overwriding methods in the subclass because is the "final" version. You have to use it as it's.
While a private method it doesn't appear in the subclasses so it can't be hidden or overriden. In that way is implicitely final.
Originally posted by Ragu Sivaraman:
Good point Manish and good example too
From Jose's comment:
While a private method it doesn't appear in the subclasses so it can't be hidden or overriden. In that way is implicitely final
So private final void foo()
final modifier is redundant becoz all private methods are implicitly final
so when you define another method in your subclass
public(or whatever) void foo() there is no correlation betn this method and foo() defined in your superclass. So in no way you are defeating the ability of not subclassing a final method
In otherwords (private plays the primary role as an modifier than a final).. Similar anology would be
interface II {
public abstract void m(); //public abstract are unnecessary
//but compiler wont complain or care
}
Does that help you?
Ragu
SCJP2. Please Indent your code using UBB Code
Originally posted by Jose Botella:
The confusion stems from attributing the characteristic of inheritance to a final method. But this is not what the JLS tell us about final. Just think of a final method as one that if inherited it can not be modified. Don't think a final method should always be inherited and then can't be modified.
JLS could have helped a bit more here.
Consider Paul's rocket mass heater. |