Hi Bijo,
I hope I don't add more confusion, I used to teach Object Pascal, so I hope I can transfer some of the knowledge.
Think of interface and implementation as areas of scope or visibility. Any code or definitions that you put in your interface section is accessible from other units. While code or definitions that you put in your implementation section is only accessible/visible to things within that, and only that unit.
For example:
[pascal]
unit MyFirstUnit;
interface
procedure MyVisibleMethod1;
procedure MyVisibleMethod2;
implementation
procedure MyInvisibleMethod1;
begin
// Do the lambada here
end;
procedure MyVisibleMethod1;
begin
// Call Method declared in my implementation section
MyInvisibleMethod1;
// Call Method declared in interface section
MyVisibleMethod2;
end;
procedure MyVisibleMethod2;
begin
// Do the funky chicken here
end;
end.
[/pascal]
[pascal]
unit MySecondUnit;
interface
procedure MyPhatMethod1;
implementation
uses
MyFirstUnit; // If this is not here we cannot access the visible methods
procedure MyPhatMethod1;
begin
// Now use the Visible Method from MyFirstUnit
MyFirstUnit.MyVisibleMethod1;
MyFirstUnit.MyVisibleMethod2;
// The following method call will not work ( will cause a compilation error
// because MyInvisiblerMethod1 is not accessible from this unit as it was
// declared in the implementation section of the previous unit
MyFirstUnit.MyInvisibleMethod1;
end;
end.
[/pascal]
You'll notice the I prepended each method call with MyFirstUnit and you are probably thinking man that is a pain, and you would be correct. But you actually don't need that there, I put it there to clarify my point of exactly which method we are calling. That notation is used in the rare cases where 2 units might have a method with the same name, so to avoid ambiguity you specify the unit name so that the compiler knows exactly which one is meant to be called at that point.
I was going to type some stuff as to the reasons why you would put code in one part an not the other, but I think it is important to understand the concept of scope first before discussing the whys and why nots of putting code in the interface or implementation sections.
I hope this helps rather than confuses.
Bookmarks