5

In this question you see that is possible to create an abstract class adding the abstract keywrod. I am translating a project in Delphi but I see that it allows the creation of an abstract class. This is the code:

type
 TCableSPF = class abstract
  //code
end;

This is an abstract class of course and I have a lot of subclasses that implement it. By the way I see that is it possible to create an instance of this like;

a := TCableSPF.Create;

When I try to call its methods that are virtual and abstract I get the error and this is ok but can I prevent the creation of the class? Or Delphi allows this by default? thanks for the help

2
  • 1
    This is a known issue: quality.embarcadero.com/browse/RSP-10235 Commented May 28, 2017 at 13:28
  • After some Googling, I found an option {$WARN CONSTRUCTING_ABSTRACT ERROR}, however, I took a look at RADStudio's Tokyo documentation (referenced in the link showed by @SebastianProske), and unfortunately found the following note: "Delphi allows instantiating a class declared abstract, for backward compatibility, but this feature should not be used anymore". :-/ Commented Jan 20, 2018 at 0:56

1 Answer 1

9

The class abstract is a holdover from the Delphi for .Net days.
For reasons unknown there is no (current) implementation behind this keyword.

If you want to prevent instantiation of an abstract class, this keyword will not help. Instead do the following:

type
  TCableSPF = class abstract
  //code
strict protected
  //Define all constructors for an abstract class as protected.
  constructor Create; virtual; reintroduce;
end;

By defining all constructors as protected, only descendent object can access the constructor, other code cannot access the constructor.
Because you reintroducing the virtual constructor, you also cannot instantiate it using:

unit A; 

type
  TMyAbstractObject = class abstract(TObjectX)
  strict protected
    constructor Create; virtual; reintroduce;
  end;

...
unit B;

  TMyClass = class of TObjectX;

  AbstractObjectInstance = TMyClass.Create;  //Will not work for TMyAbstractObject 

Note that you should not declare the constructor override. Instead declare it virtual reintroduce (or just reintroduce if you don't want to allow virtual constructors).

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you very much. That is the same thing that C++ does because it doesn't have the abstract keyword. Btw, I have to use a workaround I see. I hope they will fix this because it is annoying :/
why should I reintroduce and not override? Is that because the definition that abstract has no implementation?
There was implementation behind it in the .net compiler.
@EmmaRossignoli, declaring it override defeats the whole purpose because then you can instantiate a hidden constructor using a class of class constructor (what other languages call a class factory construct) using the virtual nature of the constructor.
Not necessarily, @Victoria. Creating an abstract class via a metaclass variable doesn't cause the compiler to issue a warning.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.