Abstract classes vs interfaces, casting from void*

wjoe invalid at example.com
Wed Aug 14 11:32:02 UTC 2019


On Saturday, 10 August 2019 at 08:20:46 UTC, John Colvin wrote:
> On Friday, 9 August 2019 at 13:39:53 UTC, Simen Kjærås wrote:
>> <snip>
>
> Thanks for the extra detail.
>
> Is there a solid reason to ever use an interface over an 
> abstract class? (Other than multiple inheritance).
>
> I'm such a noob at anything related to OO.

The way I look at it: an interface is a guarantee. By saying

interface A { void foo()}
class B: A {}
class C: A {}

void bar(A a) {}

you guarantee that every class that implements interface A has a 
function foo and
and bar can be called with anything that implements interface A.
This is completely unrelated to inheritance.

An abstract class is an implementation, most likely a base class. 
By saying

abstract class A {void foo()}
class B: A {}
class C: B {}

void bar(A a) {}

you express that you want bar to be called with any subclass of A.

e.g. a bitmap button inherits from button which inherits from 
abstract class widget and implements the signal/slot interfaces.

Another pattern is design by introspection.
An interface defines what something _is_,
by introspection defines what something _CanDo_.
That's to say: I don't care if foo is actually (or inherits from) 
an InputRange as long as it behaves like it.


More information about the Digitalmars-d-learn mailing list