0

I writing a small Snake game in Lazarus, and Lazarus complains when I write

type
  ISegment = interface(IRenderable, IMover)
  end;

When I'm trying to achieve is to make ISegment a combined interface, but it doesn't seem to work. Does Lazarus not support multiple interface inheritance?

Undreren
  • 2,811
  • 1
  • 22
  • 34

3 Answers3

3

There is no multiple inheritance supported in the language. A class cannot be derived from multiple base classes. An interface cannot be derived from multiple base interfaces.

What you can do however, is have a class that implements multiple interfaces. Like this:

type
  TMyClass = class(TInterfacedObject, IFoo, IBar)
    ....
  end;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

It does, you just need a better reading skill to understand this (look at the syntax diagram, in the heritage part). class type identifier is not stated as optional, but implemented interface does. It's roughly read as:

"A class may extend a base class and implements as many interfaces as possible. When an interface is about to be implemented, the base class must also be specified. The other way around does not apply, you can perfectly have a class extends a base class without specifying any interface"

LeleDumbo
  • 9,192
  • 4
  • 24
  • 38
  • 1
    I'm not sure how that answers my question. I want to create an interface called `ISegment`, and I want to enforce that any class that implements it also implements `IMover` and `IRenderable`. – Undreren Jul 25 '15 at 18:00
  • 1
    I'm not trying to create a _class_ that implements multiple interfaces, I'm trying to make an _interface_ that's an extension of multiple _others_. – Undreren Jul 25 '15 at 18:01
  • ah, I see. in that case, use the same method above on http://www.freepascal.org/docs-html/ref/refse39.html page. well, the syntax diagram doesn't seem to allow it for whatever reason. – LeleDumbo Jul 25 '15 at 18:10
-1

The answer is no, Pascal is not supposed to support multiple inheritance so I don't see why it should do a different thing for interfaces then

As explained in previous answer, you still can implement several interfaces in a class

Jade
  • 301
  • 1
  • 4
  • 11
  • Interfaces are a form of multiple inheritance. – Marco van de Voort Jul 29 '15 at 07:30
  • 1
    Sorry Marco, but the question is about if interfaces can inherit more than one interface at the same time, and the answer is no, because Pascal doesn't support multiple inheritance at all. You cannot say that "interfaces are a form of multiple inheritance" because it's not multiple inheritance even if you can achieve something similar to. If it were a "form of multiple inheritance" I would not need to implement every function that comes from the interfaces each time I have to use, there is NO code reused at all (among other things). My answer is correct, but your comment isn't completely true – Jade Jul 30 '15 at 14:02
  • In addition, could I ask you how to use interfaces to achieve multiple interface inheritance directly? – Jade Jul 30 '15 at 14:03
  • Jade: sorry, but multiple inheritance is inheriting from two hierarchies. Various languages have various limitations to deal with the problems from that. In Delphi's case they chose for abstract interfaces. Nowhere is written that code must actually be rewritten. And yes you can actually inherit code too, but by using composition. e.g. see http://stackoverflow.com/questions/3483680/delphi-how-delegate-interface-implementation-to-child-object – Marco van de Voort Jul 30 '15 at 15:05
  • So you are calling "multiple inheritance" to certain mechanisms that simulate or substitute it, but they aren't – Jade Aug 01 '15 at 19:27
  • As I said, the question wasn't if you can achieve something like multiple inheritance by other mechanisms, it was about the language supporting one interface extending two or more other interfaces and I answered that the language doesn't support it. This discussion is useless, cos you want to explain how to get something that's not but behaves "like", and I am saying that multiple inheritance as it's defined is not supported by the language, thus not multiple interface inheritance supported – Jade Aug 01 '15 at 19:33
  • Then correct your own original answer to which I reacted from "not supporting multiple inheritance" to "not support INTERFACE multiple inheritance". Because that is the subject of the difference of opinion – Marco van de Voort Aug 01 '15 at 21:14