E2040
Delphi Programming Language
Severity: ModerateWhat Does This Error Mean?
E2040 means you used a class, record, or other non-interface type in a place that requires an interface type. This commonly occurs when declaring that a class implements an interface, or when assigning to an interface variable. Fix it by ensuring the type you are using is declared as an interface (using the interface keyword).
Affected Models
- Delphi 10.4 Sydney
- Delphi 11 Alexandria
- Delphi 12 Athens
- Embarcadero RAD Studio
- Free Pascal / Lazarus
Common Causes
- Listing a class or record in the implements list of another class instead of an interface
- Assigning an object to a variable declared as an interface it does not implement
- Using the 'as' keyword to cast to a type that is not an interface
- Declaring an interface variable with a class type instead of an interface type
- Misspelling the interface name so the compiler finds a class with a similar name instead
How to Fix It
-
Check the type declaration that the error points to and verify it is declared as 'interface' not 'class' or 'record'.
An interface declaration starts with: type IMyInterface = interface — note the interface keyword after the equals sign.
-
If you want a class to implement a set of methods, declare those methods in an interface first, then list the interface in the class declaration.
Example: type TMyClass = class(TObject, IMyInterface) — the interface name goes after the base class, separated by a comma.
-
If you are assigning an object to an interface variable, check that the object's class actually implements that interface.
The class must list the interface in its declaration and implement all of the interface's methods.
-
Check spelling — if the name is close to an existing class name, you may have accidentally referenced the wrong type.
Delphi interfaces are typically named with a leading 'I' prefix (IInterface, IUnknown, IMyService) to distinguish them from classes.
-
If you are using the 'as' operator to query an interface from an object, ensure the variable type is a class or interface (not a primitive).
Example: (MyObject as IMyInterface).DoSomething — MyObject must be an object reference for this to work.
When to Call a Professional
This is a compiler error — no professional repair needed. Fix it by reading the error message and correcting the code. The Delphi IDE highlights the exact line causing the problem.
Frequently Asked Questions
What is a Delphi interface and how is it different from a class?
An interface is a pure contract — it defines method signatures but contains no implementation and no data fields. A class provides the actual implementation of those methods. Interfaces allow different classes to be treated the same way through the interface type, enabling polymorphism without requiring a common base class.
Do Delphi interfaces need a GUID?
A GUID is required if you want to use the 'as' operator to query the interface from an object at runtime (QueryInterface). If you only use the interface through a variable of that interface type and do not need runtime type queries, the GUID is optional. Best practice is to always add a GUID using Ctrl+Shift+G in the IDE.
Why does Delphi use interfaces instead of just multiple inheritance?
Delphi classes support single inheritance only — a class can have exactly one base class. Interfaces fill the gap by allowing a class to implement multiple contracts without the complexity and ambiguity of multiple inheritance. This is the same pattern used by Java and C# for the same reasons.