Click here to Skip to main content
Click here to Skip to main content

Hide the Utter "Create" Constructor of TObject in Delphi

, 30 Dec 2012
Rate this:
Please Sign up or sign in to vote.
A way to hide the Create constructor of TObject in Delphi

Introduction

In Delphi, constructors can be inherited; this doesn't happen in Java, C# and C++ for example. Furthermore, constructors in Delphi can have multiple and different names; usually they are called Create, but this is just a convention, since you can define a constructor with whatever name you choose.

In addition to all this, all classes in Delphi inherit ultimately from TObject, which contains a public parameterless constructor, named Create.

Due to the facts above, it‘s easy to understand that all classes in Delphi have a public Create parameterless constructor, that has been inherited from TObject.

I am not going to discuss here whether this is bad or good. What I want to show you is a way to hide the public Create parameterless constructor of TObject in case you need to do so.

Burn this out: You cannot hide any member (field, method, constructor, destructor) in Delphi by decreasing the level of visibility. If a member in a superclass is public, you cannot hide it in a child class by changing the visibility to protected or private. Once public, you are always public. This means, you cannot hide the public Create parameterless constructor of TObject by lowering its visibility in an inheriting class.

So, how can we hide the Create constructor of TObject? Is there even a way for doing so? Yes, there is a way. We came to the solution in the LinkedIn’s Delphi Professionals group. I thought it would be worthy to share this with the rest of the Delphi community.

Basically, you can hide the public Create parameterless constructor of TObject with another public method having the same Create name. For example:

Class Definition Snippet

TSomeClass = class(TObject)
 
public
  //this constructor takes two parameters, and hides the TObject.Create()
  constructor Create(aParameter1: string; aParameter2: Integer ); 
end;

Consuming Code Snippet

var
  SomeObject: TSomeClass;
 
begin
  SomeObject:= TSomeClass.Create; //This does not compile!

  //This compiles. Uncomment and try...
  // SomeObject:= TSomeClass.Create('Hello People!', 12);

  try
    //TODO
 
  finally
    SomeObject.Free;
 
  end;
end;

See, in the code above, the TObject.Create() has been hidden. Smile | :)

There is another consideration though: What happens if we overload the Create constructor?

TSomeClass = class(TObject)
public
  //this one takes two parameters, and hides the TObject.Create()
  constructor Create(aParameter1: string; aParameter2: Integer ); overload;
  constructor Create(aThisTakesAChar: Char); overload;
  constructor Create(aThisTakesAnInteger: Integer); overload;
end; 

Now the TObject.Create() constructor is hidden again.

Consuming Code

var
  SomeObject: TSomeClass;
 
begin
  SomeObject:= TSomeClass.Create; //This does not compile!

  //This compiles. Uncomment and try...
  // SomeObject:= TSomeClass.Create('Hello People!', 12);

  //This compiles. Uncomment and try...
  // SomeObject:= TSomeClass.Create1('H'); 

  //This compiles. Uncomment and try...
  // SomeObject:= TSomeClass.Create2(12); 

  try
    //TODO

  finally
    SomeObject.Free;
  end;
end;

Why would someone want to hide the TObject.Create() anyway? It depends on the situation. I have found this very useful when implementing a singleton class in Delphi. For details, refer to Singleton class in Delphi.

As a conclusion, you can hide the TObject.Create() constructor by defining a new public method with the name Create in the inheriting class. You cannot hide TObject.Create() by lowering the visibility to protected, private, etc.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

yanniel
Software Developer Digital Rapids
Canada Canada
My name is Yanniel Alvarez Alfonso. I was born in San Antonio de los Baños, Havana Province, Cuba on October 24th, 1982.
 
I majored in Information Technology Engineering at José Antonio Echeverría Polytechnic Institute (CUJAE) in Havana City, Cuba (July 2006). After that, I got a Masters Degree in Applied Computer Science at the same University (May 2009).
 
I used to work as a professor of Information Technology at CUJAE. Right now, I work as a Software Developer in Toronto, Canada. I moved to Canada under the Skilled Worker Program on February 26th, 2010.
 
This is my personal blog: Yanniel's notes; in which I write about miscellaneous topics.
 
The link at the end of this sentence compiles an index of all the articles I have written so far about Delphi Programming.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 31 Dec 2012
Article Copyright 2012 by yanniel
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid