reimplementing an interface in a derived class

Neia Neutuladh neia at ikeran.org
Fri Jan 4 02:13:27 UTC 2019


On Fri, 04 Jan 2019 00:19:05 +0000, Alex wrote:
> B.foo overrides A.foo. By casting a B object to be an A object, A's
> behavior should be granted, shouldn't it?

I can't think of a single class system that works like that. C++, Java, 
C#, Dart, and TypeScript all work like D here. GObject in C works like D.

The point of OOP is that a bundle of data has particular ways of dealing 
with it. B has different data (at least theoretically), and that data has 
different ways of working with it. So if casting to a base class changed 
something to use the base class's behavior, you'd get bugs almost anywhere 
you used inheritance, since the derived class's data isn't being modified 
properly.

The only situation in which you might possibly want that sort of behavior 
is if inheritance were only for ease of implementation, but then you'd 
want to disallow that sort of cast anyway. It would be like trying to cast 
an object to one of its fields.


More information about the Digitalmars-d-learn mailing list