DAX FAQs 1.0 by Conrad Herrmann 27 May 97 ------------------------------------------------------------------- Q: How do I override an interface method that's already implemented in a superclass with a static method? A: Many of the DAX class implementaitons are implemented this way. Because of the interface method is bound to a static method in the ancestor class, you can't simply override the method as you would a virtual method. Following normal Delphi behavior, a static method cannot be overridden in a polymorphic way: you must reimplement it. The vtable of an interface in the ancestor class is bound to the static methods introduced in that class. When you reimplement the static method, you must also reimplement the interface. This allows the compiler to generate a new vtable for the interface, with methods bound to the sub-class' methods. You can reimplement the interface by listing it in the class' interface list. Example: This example is implemented in the DOER sample. type // This is the declaration of an interface with two methods. IDoer = interface procedure DoThis; procedure DoThat; end; // This class implements both methods of the IDoer interface. TFoo = class(TInterfacedObject, IDoer) procedure DoThis; procedure DoThat; end; // This class reimplements DoThat but does not reimplement the interface TBar = class(TFoo) procedure DoThat; end; // This class reimplements DoThat and also rebinds the interface, // binding the interface to the new implementation of DoThat TBar2 = class(TFoo, IDoer) procedure DoThat; end; procedure TFoo.DoThis; begin Form1.Memo1.Lines.Add('TFoo.DoThis'); end; procedure TFoo.DoThat; begin Form1.Memo1.Lines.Add('TFoo.DoThat'); end; procedure TBar.DoThat; begin Form1.Memo1.Lines.Add('TBar.DoThat'); end; procedure TBar2.DoThat; begin Form1.Memo1.Lines.Add('TBar2.DoThat'); end; If you find yourself buildidng a class hierarchy where one or more interface methods are being regularly overridden, you might want to declare the base implementation of that method as virtual. this connects the interface method to the virtual method, meaning the method overridden by the subclass. Example: type // This is the declaration of an interface with two methods. IDoer = interface procedure DoThis; procedure DoThat; end; // This class implements both methods of the IDoer interface, and // declares DoThis virtual. TFoo = class(TInterfacedObject, IDoer) procedure DoThis; virtual; procedure DoThat; end; // This class overrides DoThat TBar = class(TFoo) procedure DoThat; override; end; Declaring methods as virtual is slightly more expensive than declaring them static, both because it causes each class' vtables to grow, and because it requires an extra lookup to dispatch the function call. Because the DAX TActiveXControl class implements a large number of interfaces, the DAX class hierarchy implements its interface methodss as static. This makes the vtables of the subclasses much smaller, especially since developers will rarely want to override any of these implementations. If you really do want to override an interface method that was implemented in TActiveXControl, you can use the technique described above.