Let’s say I have a program to write text into a file (not really but that’s easier to explain). I want a seperate class for each filetype like PdfType and WordType that inherit from a FileTypeMaster.
FileTypeMaster (base)
-PdfType : FileTypeMaster
-WordType : FileTypeMaster (same methods as pdftype but works different)
Now to the real problem… I want the user to decide on programstart what type to use.
If he wants Pdf OR Word the methodcall should look the same (because word is new and the program was just for pdf before).
How it should work for example with pdf:
static FileTypeMaster MyFavoriteType; //declare a general var
MyFavoriteType = new PdfType(); //cast general var to the wanted type
MyFavoriteType.CompileThis();
How it should work with word:
the same but MyFavoriteType = new WordType();`
Interfaces are made for that.
MyFavoriteType should be an interface, with anything you will ever need (functions and proprieties) built in.
Then each wanted type will implement that interface and do their own thing.
Here is a small example:
You can now do something like so:
And this will work for ANY SINGLE CLASS that correctly implements
Polygoninterface.So this is possible:
In your case, the interface would look like so: