Overloading Methods

A method can be redeclared using the overload directive. In this case, if the redeclared method has a different parameter signature from its ancestor, it overloads the inherited method without hiding it. Calling the method in a descendant class activates whichever implementation matches the parameters in the call.

If you overload a virtual method, use the reintroduce directive when you redeclare it in descendant classes. For example, type

T1 = class(TObject)

procedure Test(I: Integer); overload; virtual; end;

procedure Test(S: string); reintroduce; overload; end;

SomeObject := T2.Create;

  1. Test('Hello!'); // calls T2.Test
  2. Test(7); // calls Tl.Test

Within a class, you cannot publish multiple overloaded methods with the same name. Maintenance of runtime type information requires a unique name for each published member.

type

TSomeClass = class published function Func(P: Integer): Integer;

function Func(P: Boolean): Integer; // error

Methods that serve as property read or write specifiers cannot be overloaded.

The implementation of an overloaded method must repeat the parameter list from the class declaration. For more information about overloading, see Overloading procedures and functions.

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment