At that time I proposed the addition of a new attribute that would restrict changes to the implementation of a class through inheritance to allow pairing of methods. Restricting the implementation of a method might also be needed for important functions for security reasons, etc. A work item 834 was created and assigned to me in meeting 209.
A new paper 98-0282 was presented with the changes suggested by the committee. There were 3 minor corrections and the document was approved with modifications. The present document 98-0293 fixes those errors.
- the FINAL attribute for classes and methods
If a class is defined with the FINAL clause, that class cannot be
used as a superclass. If a method is defined with the FINAL clause, that
method cannot be overridden in a subclass.
5) Class-name-2 shall not be the name of a class defined with the
FINAL clause.
6) If two or more different methods with the same name are inherited,
none of them may be specified with the FINAL clause. If the same method
is inherited from one superclass through two or more intermediate superclasses,
it may be specified with the FINAL clause.
3)If the FINAL clause is specified, the class cannot be the superclass
for any other class.
2) If the OVERRIDE phrase is specified, there shall be a method
with the same name as the method declared by this method definition defined
in a superclass. The method in the superclass shall not be defined with
the FINAL clause.
7) The FINAL clause shall not be specified in an interface definition.
3) The FINAL clause indicates that this method shall not be overridden
in any subclasses.
C.15.4.2 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 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 which 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.