The Override Problem

From Apache OpenOffice Wiki
Jump to: navigation, search



The term “override problem” describes a problem that occurs when a virtual function of a base object becomes unreachable because an interface method overrides the implementation of the base class. For example, all CLI objects derive from System.Object. If an interface has a method that has the same signature as one of System.Object's methods, then the respective method of System.Object is unreachable if the interface method is virtual.

For example, consider the following declaration of the interface XFoo and its implementing class :

 using namespace System;
 
 public __gc __interface XFoo
 {
 public:
         virtual String* ToString();
 };
 
 public __gc class Foo : public XFoo
 {
 public:
         virtual String * ToString()
         {
                  return NULL;
         }
 };

If the method ToString of an instance is called, then the implementation of the interface method is invoked. For example:

 int main(void)
 {
         Foo * f = new Foo();
         Object * o = f;
         f->ToString(); // calls Foo.ToString
         o->ToString(); // calls Foo.ToString
 
     return 0;
 }

This may not be intended, because the interface method likely has a different semantic than its namesake of System.Object.

A solution is to prevent the interface method from overriding the method of System.Object without making the interface method non-virtual. The CLI provides a remedy by way of the “newslot” flag, which is attached to the method header in the IL code. CLI languages may have different means for denoting a method with “newslot”.

The following examples show ways of implementing XFoo in different languages, so that Object.ToString can still be called.

  //C++
  //interface methods should be qualified with the interface they belong to
  public __gc class A: public XFoo
  {
  public:
          virtual String* XFoo::ToString()
          {
                   Console::WriteLine("A::foo");
                   return NULL;
          }
  };
Documentation note.png Although XFoo::ToString is virtual, it cannot be overridden in an inheriting class, because the CLI method header contains the final attribute. In an inheriting class one can, however, derive again from XFoo and provide an implementation.

In C# there are different ways provide an implementation:

  // IL contains: newslot final virtual
  public new string ToString()
  {
  }

The keyword new inserts the newslot attribute in the CLI method header. This implementation cannot be overridden in an inheriting class.

  //IL contains: newslot virtual
  public new virtual string ToString()
  {
  }

This method can be overridden in a derived class.

 // Using a qualified method name for the implementation. The virtual 
 //modifier is not allowed
 string XFoo.ToString()
 {
                   return null;
 }

This implementation cannot be overridden in a derived class. An instance of the implementing class must be cast to XFoo before the method can be called.

  'VB .NET
  Public Shadows Function ToString() As String Implements XFoo.ToString
           Console.WriteLine("Foo.toString")
  End Function

This implementation cannot be overridden in a derived class.

  Public Overridable Shadows Function ToString() As String _
  Implements XFoo.ToString
          Console.WriteLine("Foo.toString")
  End Function

This method can be overridden.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages