Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of creating an abstract class function

Tags:

delphi

In Delphi I often see code like this :

TmyClass = class
public
   class function getSomething: integer; virtual; abstract;
end;

But what is the purpose of such declaration (ie: class function), because calling TmyClass.getSomething will always fail as it is not implemented, even if it is implemented in a child class.

like image 840
zeus Avatar asked Jan 25 '23 15:01

zeus


1 Answers

It fails if you call TmyClass.getSomething directly, but it can be useful in combination with metaclasses. It gives you opportunity to define abstract API just like it would on non class functions.

For instance:

TmyClassClass = class of TMyClass;

TmyClass1 = class(TmyClass)
public
   class function getSomething: integer; override;
end;

var
  c: TmyClassClass;

  c := TmyClass1;
  c.getSomething;

Of course, you can always use class functions on object instances, so calling getSomething on TMyClass1 object instance will work, too.

like image 70
Dalija Prasnikar Avatar answered Feb 05 '23 21:02

Dalija Prasnikar