Restricting inheritance and modification with the FINAL clause
It may be desired that no extension be done to a class through inheritance. This, for example, might be needed by library providers who want to control the implementation of a class. To provide this functionality, a class may be declared 'final' if its definition is complete and no subclasses are desired or required. A compile-time error occurs if the name of a final class appears in the INHERITS clause of another class declaration; this implies that a 'final' class cannot have any subclasses.
Because a final class never has any subclasses, the methods of a final class are never overridden. This may be an overkill in some cases, and it might be desired that only a few methods not be overridden. For that purpose, we can use the attribute 'final' with a method of any class, to prohibit the subclasses of that class from overriding that method. This attribute can also be used redundantly with the methods of a final class.
Restricting methods from being overridden also helps in the 'pairing' of methods of a class. Here's an example: Suppose a class A defines a method 'bar' calling another method 'foo' defined in the same class. If there is a subclass B that also defines 'foo', and if we invoke 'bar' on an object of class B, the method 'bar' (inherited from class A) will end up calling the function 'foo' defined for B and not the original 'foo'. If however, we specify the FINAL clause with the function 'foo' in class A, it cannot be overridden in class B, and the programmer can ensure that 'bar' will always invoke the same 'foo'.
The FINAL attribute has to be handled carefully while dealing with multiple inheritance. If two classes A and B define a method of the same name, and if a class C inherits from both of them, the method definitions are not allowed to have the FINAL clause specified. However, if the same method is inherited through two classes which had the same superclass defining that method, the method is allowed to have the FINAL clause specified in its definition. This would happen for a diamond shaped multiple inheritance, where classes B and C inherit from a class A, and then class D inherits from both B and C. Class A can have methods with the FINAL clause specified, and though D will appear to inherit two methods of the same name with the FINAL clause, it is acceptable as they are the same method implementations.