Comparison and contrast of interfaces in Java and Delphi

I am a Java developer who has recently been pulled into the Delphi developer hat.

As is usually the case in such situations, I end up trying to do something in Delphi, still use the “Java” mindset, and I get embarrassed when they don't work.

Today, the problem is the concept of an interface. In Java, I can define an interface, give it several methods, and then declare a class that implements this interface.

I tried to do the same in Delphi, and my fingers were burned. I announced an interface that extended IInterface. But when it came time to implement this interface, I was met by a number of unfulfilled methods errors for methods that I did not declare (QueryInterface, _AddRef, _Release).

Little Google told me that I need to extend TInterfacedObject instead of TObject. This caused me concern because it suggests that I cannot just add an interface to a third-party class if this class does not extend TInterfacedObject.

But now that it's time to set up my paired object. Free, I get EInvalidPointer exceptions.

As a result, I begin to conclude that the word interface means something completely different than the Java developer and the Delphi developer.

Can someone who speaks both languages ​​educate me about the differences?

Greetings.

+4
source share
2 answers

Interface types in Delphi have three functions:

  • a la Java.
  • ​​ Delphi COM (Delphi IInterface COM IUnknown).
  • , .

, , , :

  • Delphi , , IInterface/IUnknown , , IUnknown (AddRef, Release QueryInterface - _AddRef _Release Delphi, ). , GUID.
  • TInterfacedObject , , _AddRef, _Release QueryInterface ( , ). , -1 (, TComponent).
  • , _AddRef _Release, . , _AddRef _Release -1.
+7

. Java . Delphi .

_AddRef _Release , . , Delphi _AddRef. . "" Delphi _Release. . 0, destroy() Self.Destroy().

, .

PS. TInterfacedObject , Google Delphi TInterfacedObject.

+2

All Articles