Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>No. There is no way to force code outside your control to <em>not</em> call something that is otherwise perfectly accessible. The best you can do is strongly discourage the practice in the documentation for the class.</p> <p>What are the consequences if a descendant calls the inherited method? If it means the program stops working, then so be it. The programmer who writes the descendant class will test the code, notice that it doesn't work, and then consult the documentation for the method to ensure that he's using it correctly (at which time he'll learn he isn't).</p> <p>You could take another approach. Instead of making the function virtual, and having descendants override it, provide a protected method-pointer property.</p> <pre><code>type TGetFileImpl = procedure of object; TAncestor = class private FGetFile: TGetFileImpl; protected property GetFileImpl: TGetFileImpl write FGetFile write FGetFile; public procedure GetFile; // not virtual. end; TDescendant = class(TAncestor) private procedure SpecializedGetFile; public constructor Create; end; procedure TAncestor.GetFile; begin if Assigned(GetFileImpl) then GetFileImpl else begin // Do default implementation instead end; end; constructor TDescendant.Create; begin GetFileImpl := SpecializedGetFile; end; </code></pre> <p>The base class provides a method pointer that descendants can assign to indicate they want their own special handling. If the descendant provides a value for that property, then the base class's <code>GetFile</code> method will use it. Otherwise, it will use the standard implementation. Define <code>TGetFileImpl</code> to match whatever the signature of <code>GetFile</code> will be.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload