M3 OO Style For Safety

When writing object-oriented code in Modula-3, there are a set of rules that should be followed to help ensure type and system safety. In each of the following examples we compare some bad OO code to some good OO code. The pages in the following section all use frames, so use a frame-enabled browser.

Rules

Modula-3 does not distinguish between class interfaces and implementations. A class interface, which declares the methods that a class exports, should be declared inside a Modula-3 INTERFACE, and should not declare default methods. A class implementation should override all of the methods to their values. Otherwise, objects can be treated as their supertypes, and have all sorts of methods called on them.

Method extension should be avoided. It is very dangerous, because the extended method remains part of the subclass interface.

Partially revealed types should be BRANDED, so that the types cannot be spoofed.

Check all of the revelations of a superclass. If you create a subclass, then any method of the superclass is in your interface.

Override any superclass method that could do bad things to your subclass. A non-overridden method is effectively in thesubclass interface.


Last changed July 17, 1996
whsieh@cs.washington.edu