We've finished the previous article on inheritance talking about the way used in FreePascal (and in Delphi too) to make a class abstract; we've learned that by declaring just one method as virtual abstract is enough to get the result.
But again we've seen that is not true, indeed.
Theory says one thing, and reality another one! This is a rule you should always keep in mind, because reality always doesn't care of your thoughts. 😉
Back to our example... is there a way to get a true abstract class? Luckily yes!
Ladies and Gentlemen, here the interfaces!
An interface is "simply" what we've just said, a pure abstract class. Look at it like an empty holder.
But the difference with the so declared abstract class is that this one works! beautiful!
Again: don't trust us, and try yourself if this is true. Take a piece of code with a bunch of it building an interface, compile and see if the compilation finishes, or if you'll run a compiling error, with the specification of the missing implementation of the interface.
And beware of this: when we talk of interfaces and of their redeclaration, we intend the redaclaration of all its methods.
It simply works as abstract class.
So let's try to build one of this, by keeping our familiar employee's example.
Well... in this case it's not so important if we deal with employee or not, because we're going to write an interface where we just put the wrapping to redeclare the sex of a person.
unit Human; {$mode objfpc}{$H+} interface uses Classes,SysUtils,Dialogs; type IHuman = Interface //Human is an INTERFACE function GetSex():string; procedure SetSex(Sex_:string); end; implementation end.
Here above a possible correct interface.
Yes! It can be so short.