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.
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;
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With