Monday, 30 September 2013

Compare and contrast interfaces in Java and Delphi

Compare and contrast interfaces in Java and Delphi

I am a Java developer who has recently been thrust into wearing a Delphi
developer hat.
As is typically the case in such situations, I wind up trying to do things
in Delphi while still using my 'Java' mindset, and I get confounded when
they don't work.
Today's issue is the notion of an interface. In Java, I can define an
interface, give it some methods, and later declare a class that implements
that interface.
I have tried to do the same thing in Delphi, and got my fingers burned. I
declared an interface that extended IInterface. But when it came time to
implement that interface, I was greeted by a number of unimplemented
methods errors for methods I didn't declare (QueryInterface, _AddRef,
_Release).
A little Google told me that I needed to extend the TInterfacedObject
instead of TObject. This made me uneasy because it suggests that I cannot
simply add an interface to some third-party class unless that class
ultimately extends TInterfacedObject.
But now, when it becomes time to set my interfaced object .Free, I'm
getting EInvalidPointer exceptions.
As a result, I'm beginning to conclude that the word interface means
something completely different to a Java developer, and a Delphi
developer.
Can someone who is proficient at both languages enlighten me as to the
differences?
Cheers.

No comments:

Post a Comment