13

I think this might be a useful language feature and was wondering if any languages already support it.

The idea is if you have:

class C
  virtual F
     statement1
     statement2

and

class D inherits C
  override F
     statement1
     statement2
     C.F()

There would be a keyword applied to C.F() such that removing the last line of code above would cause a compiler error because it's saying "This method can be overridden but the implementation here needs to run no matter what".

1

7 Answers 7

17

Yes, they do. It is called Scandinavian model of OO, it is used for example in Simula (the other OO model which is widespread and taken as granted now, is American model). In Scandinavian model, you are not overriding, but supplying sub-behaviour.

in Superclass's method foo:

some-code-before
INNER // this is the actual Simula keyword
some-code-after

in Subclass' method foo:

some-code-in-subclass

If you call Superclass' instances' method foo, only some-code-before and some-code-after happens (INNER does nothing), but if you call Subclass' instances' foo, it does some-code-before, some-code-in-subclass and then some-code-after.

9

No language I know of enforces calling the overridden method. Indeed, some languages allow overriding methods that are not overridable (such as using the new keyword in C#). However, there are two ways of approaching this.

The first is to create an unoverridable method (e.g. one that lacks the virtual keyword in C# or one that has the final keyword in Java) that calls an overridable one that cannot be called from outside the class (e.g. protected in C#, Java or C++).

class C
  A
     statement1
     F
     statement3

  protected virtual F
     statement2

and

class D inherits C

  protected override F
     statement4
     C.F()

Classes overriding C are free to override the F and modify its behavior but callers from outside the class only access it through A.

Edit: As others have pointed out, this is called the Template method pattern.

The second way is to use a language that enforces preconditions and postconditions specified in the base class, like Eiffel or C# with Code Contracts. It will not force the base class to be called but the overridden method can be forced to perform the same statements. Using aspects may also help if the language allows aspects to be inherited.

2
  • 2
    You can even make the method to be overriden private in C++ :) Herb Sutter explains it here in detail. Sep 26, 2012 at 6:21
  • Template pattern has only one disadvantage that you need reimplement it each time while going deeper through inheritance hierarchy. The example with Simula is more elegant and still allows for template pattern. Nov 22, 2013 at 12:41
7

Not really part of the language, but the FindBugs static code analyzer for Java has an annotation OverrideMustInvoke that a developer can add to a method, and which will cause FindBugs to show an error if it finds an overriding method that doesn't call the super implementation. It even allows specifying whether the call must be first or last in the overriding method.

7

Requiring to call a superclass method is an anti-pattern. If it isn't enforced at compile time it is error-prone, which is why you are looking for a language construct that checks for it.

There is a way that is supported in all OO languages: The template method pattern. Here, you make the superclass method not overridable, and in it you call a overridable method. The subclass then can override this method to add functionality:

class super {
  public final void doSomething() {
    doSpecialthing();
    doMore();
  }
  public void doSpecialthing() {
  }
}

Depending on the location of the call to the overridden method it even allows to determine the order of execution, which with the ordinary super call is at will of the subclass implementer.

1

The closest pattern I can think of is self-subscribed events. It's a bit cumbersome, and not at all intuitive for the coder, but achieves the goal.

class C
{
    public void F()
    {
        ...
        OnF()
    }

    protected event OnF
}

class D : C
{
    public D()
    {
        base.OnF += this.F
    }

    private void F
    {
        ...
    }
}
1
  • 1
    This is a fairly common design pattern, sometimes with both pro and post overrides eg. ViewWillAppear(), ViewDidAppear(). Ideal when you want to allow subclasses to extend (instead of modify) default behavior. Sep 26, 2012 at 5:50
1

Lisp machine "flavors" allowed methods with type "before" "after" and "around" the inherited main method.

0

While not a bad idea in theory, it does have the negative side-effect of constraining my options when implementing D. For example, what if (for some unfathomable reason), it's more convenient to call the superclass implementation of F from some other method:

class D inherits C
    override F
        statement1
        statement2
        G()
    G
        statement3
        C.F()
        statement4

Under your scenario, I imagine the compiler would flag the implementation of F in D, even though it does (indirectly) call C.F().

Basically, what you've described is a possible mechanism to help the compiler recognise when a contract on classes inheriting from C is violated. My point is that, while that's a great thing, it shouldn't come at the expense of limiting how I can implement my subclass.

2
  • 1
    -1: being able to think of a situation where you wouldn't want to use that isn't an answer to the question "does any language allow that".
    – user4051
    Sep 26, 2012 at 12:54
  • @GrahamLee: very true. My point was to try and explain one reason why no language (that I'm aware of) implements such a feature. I guess I got so caught up in explaining that, that I forgot to mention why I was explaining it. -1 happily accepted. :)
    – Mac
    Sep 26, 2012 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.