I have problem with implementing relation between some classes. I got three different classes serving three different forms. All these classes speak in same language, so they are inherited from same class called TAncestorServer. Let’s call its descendants TForm1Server, TForm2Server, and TForm3Server. TAncestorServer contains an abstract method called Function1, so TForm1, TForm2, and TForm3 can call it with no problem through their own classes inherited from it, they have an access to the classes through a property named Server. But the problem is about another form called TForm4! It’s very similar to other forms but it’s not independent like them. It works with TForm2Server or TForm3Server. Still not a problem, but imagine another method like Function2 which is declared in TForm2Server and TForm3Server, TForm4 needs to call them. I can go like this:

if Server is TForm2Server then
TForm2Server(Server).Function2
else if Server is TForm3Server then
TForm3Server(Server).Function2;

But it can turn in an infinite if-else clause! So I thought something like multiple-inheritance may help here. I declared an interface called IForm4Server which contains Function2. So TForm2Server and TForm3Server inherit from both TAncestorServer and IForm4Server. I thought something like this could work:

If Server is IForm4Server then
IForm4Server(Server).Function2;

But compiler doesn’t think so, it says that it’s not a valid type cast, because TAncestorServer is not IForm4Server, which is absolutely right. TForm1Server has no idea about implementing Function2 and has to leave it blank. I can’t declare TForm4.Server as IForm4Server too, because Function1 represents a huge number of methods and properties, however I still can’t type cast IForm4Server to TAncestorServer.

As a solution I can define two different properties in TForm4 like GeneralServer: TAncestorServer and Form4Server: IForm4Server and then assign same instance of TForm2Server or TForm3Server to them, but I don’t feel good about it. How should I do this? Is there any standard pattern for it?

About the title of your question, a Interface is not an ancestor of your class. Delphi supports single-inheritance only (as you seem to know), so every class have only one father/grand father.. and so on. So, you only inherit methods from the ancestor. Your class can implement any number of interfaces, but the interfaces are not ancestors of your class, just interfaces it implements.
–
jachguateFeb 20 '13 at 22:22

Thanks for the correction. I’m new in both using interfaces and asking questions in English! Any suggestions would be appreciated.
–
saastnFeb 20 '13 at 22:51