Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping a function from been overridden in Delphi

Tags:

oop

class

delphi

How do I stop a function/procedure in a superclass from been overridden in a subclass in Delphi (2007)?

I want to mark it so it can not be altered, I believe there is a final keyword but can not for the life of me find the documentation for it, so I am not 100% sure that's what I need.

like image 309
Re0sless Avatar asked Oct 03 '08 14:10

Re0sless


2 Answers

The keyword is final as you thought. See http://dn.codegear.com/article/34324 and http://blogs.teamb.com/rudyvelthuis/2005/05/13/4311. Also you can mark your class as sealed to prevent anyone from inheriting from it. You need a Delphi version higher than 7.

type
  TSomeClass = class
  protected
    procedure SomeVirtualMethod; virtual;
  end;

  TOtherClass = class(TSomeClass)
  protected
    procedure SomeVirtualMethod; override; final;
  end;
like image 88
Lars Truijens Avatar answered Oct 03 '22 22:10

Lars Truijens


You're right - it's "final". This snippet shows it. (from one of Marco Cantu's books)

type
  TDeriv1 = class (TBase)
    procedure A; override; final;
  end;

  TDeriv2 = class (TDeriv1)
    procedure A; override; // error: "cannot override a final method"
  end;

Compiling gives:

[Pascal Error] Unit1.pas(11): E2352 Cannot override a final method

One thing that surprised me: This feature is supported in Win32 Delphi, not just Delphi for .NET

like image 39
Roddy Avatar answered Oct 03 '22 23:10

Roddy