tags:

views:

486

answers:

3

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.

+13  A: 

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;
Lars Truijens
+9  A: 

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

Roddy
A: 

@Roddy,

Most languages features of Delphi.Net is being ported to the win32. That include enumerators, for-in, sealed classes, class helpers and such.

That include, as you saw, final methods.

Fabricio Araujo
This was true up until Delphi 2007. Nick Hodges has stated that there will no longer be any effort to keep Delphi Win32 and Delphi .Net compatible. Code Gear have decided that this is restricting both products and that there are very few developers working in both .Net and Win32.
Richard A
I know this, but it really brought some new things to the native world. Of course, it's interesting that features of other environments come to the pascal language - specially if they make things clearer and better - but doesn't need to be exactly equal... ;-)
Fabricio Araujo