An object in Delphi is actually a 4 byte long pointer to a structure in the memory (on the heap). At the beginning, the pointer contains a random adress (often
nil, but it could be whatever value the register had before). So we have to create the structure in the memory first and then remember the adress of this structure in the variable.
We do this by writing something like this:
Delphi-Quellcode:
var
MyStringlist: TStringlist; // declare the variable stringlist as an instance of a tstringlist-class.
begin
MyStringlist := TStringlist.Create; // Create the Stringlist-object
// Now we can use the object
MyStringlist.LoadFromFile('readme.txt');
MyStringlist.Free; // Destroy the object and free the allocated memory
end;
A class (e.g. TStringlist) describes "what the object does", which properties and which methods it has. Classes are something like "construction plans" for objects. If we build the object described in the construction plan, we call this object an instance (of the class).
So, one of the coolest things about
OOP is, that you can extend (or inherit) classes. When declaring a new class, you can tell Delphi to build this class based on a class that already exists (the parent). Now the new class contains all methods and properties of the parent plus some extra features. Because the child contains everything of the parent (the classes are compatible), you can assign instances of child classed to their parent classes. This works only in one direction of course.
For example the TStrings-class has basic properties and methods to work with text. The TStringlist-class extends the class by adding some methods like sorting and saving the data to a file and loading it (i think).
Now we can do something like this:
Delphi-Quellcode:
procedure DoSomething(Strings: TStrings); // Note that the parameter is declared as TStrings
begin
Strings.Clear; // deletes the text contained in "Strings". Just some random example...
end;
var
MyStringlist: TStringlist; // Note that we use a TStringlist
begin
MyStringlist := TStringlist.Create;
MyStringlist.LoadFromFile('readme.txt');
DoSomething(MyStringlist); // This works now, because TStrings is the parent of TStringlist, therefore TStringlist has everything TStrings has.
MyStringlist.Free;
end;
This gets really interesting when it comes to overriding methods. "Strings.Clear" in line 3 is a bad example because it calls the same method for both TStrings and TStringlist (TStringlist doesn't override it). But classes can also override methods of their parents (if the method is declared as "virtual" in the parent class). Then one line can call different methods depending on which class the object is an instance of.
Look at the TGraphic class. The base class doesn't even do anything except declaring the methods used by the children (TBitmap, TJPEGImage and so on...). All codes for loading the image formats are implemented in the child classes and override the methods of the base class. This way, we can work with graphic without knowing which format they have.
Now because most of the methods of the base class don't contain any code, they are marked as "abstract". When trying to call them directly, you get an
exception stating something like "abstract error".
A more flexible alternative to those only abstract objects are interfaces. Interfaces may not contain code. They only describe which functionality an object must have. This way a program (or a programmer) doesn't have to care about each individual implementaion as long as it supports the interface. Whatever the object behind the interface is, it doesn't matter, the code stays the same.
This is how I understand interfaces.
What you have done, is, you have declared some variables as interfaces (IMessenger, IMessengerContacts, IMessengerContact), but you haven't assigned any instances to them. Remember, interfaces only describe the functionality, but they don't contain any themselves.
I hope you understand my explanation. Please note that I haven't really worked with interfaces yet myself (except for reading out
XML files). If I left out something, please search for a tutorial about
OOP, I'm sure there are plenty of them on the internet.