Document: J4/98-0293
98/04/30
 

FULL REVISION WORKING PAPER WR-834.2

Subject:  FINAL attribute for classes and methods

Date:      April 30, 1998

Author:   Virendra K. Mehta

References:

Proposed Revision

Add a FINAL clause to the class definition and the method definition to prevent them from being altered through inheritance. A class defined with the FINAL clause will not be allowed to be a superclass for another class. A method defined with the FINAL clause will not be overridden in a subclass.

Justification

This was done in response to public review comment 9 (doc# 96-0474). Additionally, Takagi's comment in ISO-35 - 97-0351 warranted a feature like FINAL.

Compatibility

This feature is compatible with J4 CD 1.2 and with ANSI-85.

Discussion

Initial investigation

Takagi's comment in ISO-35 in 97-0351 depicted the following scenario: 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 function (inherited from class A) will end up calling the function foo defined for B and not the original foo. This may not be desirable in certain situations and a mechanism is needed to empower the programmer to restrict a new definition of foo. This functionality is not provided in our current object model which does not support non virtual functions that some other languages like C++ support.

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.

Meeting 212

The new attribute FINAL was described in the paper 98-0200. It proposed alternate syntaxes and syntax and general rules for the CLASS-ID and METHOD-ID paragraphs. It was discussed and it was suggested that it be modified based on the discussions and submitted again.

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.

Proposed Changes to Base Document

  1. Page 149, change the class-definition in part to add the clause [ IS FINAL ] after the phrase [ AS literal-1 ]

  2.  
  3. Page 150, change the method-definition in part to add the clause [ IS FINAL ] after the clause [ OVERRIDE ]

  4.  
  5. Page 152, 10.3, External repository, insert the following bullet after bullet 7:
  6.     - the FINAL attribute for classes and methods
     

  7. Page 158, 10.4.17 Class inheritance, insert the following paragraph after the 2nd paragraph
  8. 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.
     

  9. Page 165, 11.3.1 General format, change to add the clause [ IS FINAL ] after the phrase  [ AS literal-1 ]

  10.  
  11. Page 165, 11.3.2 Syntax rules, insert a new rule 5 and reorder accordingly:
  12. 5) Class-name-2 shall not be the name of a class defined with the FINAL clause.
     

  13. Page 165, 11.3.2 Syntax rules, insert a new rule 6 and renumber accordingly
  14. 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.
     

  15. Page 165, 11.3.3, General rules, insert a new rule 3 and renumber accordingly:
  16. 3)If the FINAL clause is specified, the class cannot be the superclass for any other class.
     

  17. Page 169, 11.6.1 General format, change to add the clause [ IS FINAL ] after the clause [ OVERRIDE ]

  18.  
  19. Page 169, 11.6.2, Syntax rules, change rule 2 to:
  20. 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.
     

  21. Page 169, 11.6.2, Syntax rules, replace 'an inherited' with 'a' in  rule 3.

  22.  
  23. Page 169, 11.6.2, Syntax rules, add a new rule after rule 6:
  24. 7) The FINAL clause shall not be specified in an interface definition.
     

  25. Page 169, 11.6.3, General rules, insert a new rule 3 and renumber accordingly:
  26. 3) The FINAL clause indicates that this method shall not be overridden in any subclasses.
     

  27.  Page 706, insert a new section C.15.4.2 and renumber accordingly:
  28. 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.

Please send comments to Veeru Mehta (Veeru_Mehta@hp.com).