Abstract Classes and Final Classes in Delphi

Abstract Classes in Delphi
If a Class have at least one Abstract member then we call it Abstract Class. An Abstract member is a Class member that have only declaration on that class and the implementation of that member is expected to be done in its descendant classes. An Abstract Class can have normal methods also. In Delphi it allows to create Object of an Abstract Class but we should not create because when we try to access any Abstract member then we will get run time exceptions. And if a class is having only Abstract members then it is called as pure Abstract class.

Why we use Abstract Class?
An Abstract Class is written when there are some common features are shared by all the objects. Suppose in your project if you have some common features then declare that type of properties and methods as Abstract and you can extend wherever you want in your Module
For example...
If you want to make a new car(WagonX) in which all the another car's properties are included like color,size, engine etc.and you want to add some another features like model,base Engine in your car. Then simply you create a abstract class WagonX where you use all the predefined functionality as abstract and another functionalities are concrete, which is is defined by you. Another sub class which extend the abstract class WagonX,By default it also access the abstract methods which is instantiated in abstract class. SubClasses also access the concrete methods by creating the subclass's object.For re-usability of the code, the developers use abstract class mostly.

In Delphi we use Abstract keyword to declare an Abstract class and Abstract member. And in Delphi an Abstract method must be declared as Virtual or Dynamic and must have to Override on its child classes if we want to create an instance.
Example...
type 
  TPolygon= class(TObject)
  public
    function GetArea: integer; virtual; abstract;
end;
type 
  TRectangle = class(TPolygon)
  private
    Length, Width : integer;
  public
    function GetArea: integer; override;
end;
type TTriangle= class(TPolygon)
  private
    base, height : integer;
  public
    function GetArea : integer; override;
end;
.......
function TRectangle.Area;
begin
  result := Length * Width;
end;
function TTriangle.Area;
begin
  result := (base*height) / 2;
end;
procedure TFrom.GetShapeArea(polygon: TPolygon);
begin
  Showmessage('Area: ' + IntToStr(polygon.Area));
end;
procedure TFrom.Button1Click(sender: TObject);
begin
  GetShapeArea(RectangleObj);
  GetShapeArea(TriangleObj);
end;
On above code GetShapeArea takes TPolygon type parameter, but on Button1Clik we have passed Rectangle Object or Triangle Object. So Triangles or rectangles are types of polygons and thus for each of them we can get an area value but there are no object of the "polygon" type.

We can declare a pure Abstract Class by using Abstract keyword in class declaration.
type 
  TPolygon= class abstract (TObject)
  public
    function GetArea: integer; virtual; abstract;
end;

Delphi does not support abstract member properties directly. To implement an abstract property, make use of abstract methods. That is, you can read a GetPropertyX abstract function and write to a SetPropertyX abstract procedure. In effect, creating  an abstract property.

Sealed or Final Classes in Delphi
Generally we cannot inherit Child classes from a Sealed class. With Delphi, use the sealed keyword to prevent a class from being inherited from and use the final keyword to prevent a method from being overridden.
Example...
type
  Robot = class sealed(TObject)
  public
    procedure Speak(pSentence: String); virtual; final;
end;

Comments

Popular posts from this blog

ShellExecute in Delphi

How to send Email in Delphi?

Variants in Delphi. Use of Variant Array. How to check a Variant is unassigned or empty or clear ?