Real OOP with D
BBasile via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Aug 16 23:59:49 PDT 2015
On Monday, 17 August 2015 at 05:57:52 UTC, Ozan wrote:
> Hi
>
> Working with objectoriented concepts results often in large
> trees of related classes. Every instance of a class knows his
> methods and data. An example like following would work:
>
> import std.stdio;
> class Family { }
> class Dad : Family { void greeting() { writeln("I'm dad"); } }
> class Boy : Family { void greeting() { writeln("I'm daddy's
> boy"); } }
> void main() {
> writeln("Father and son");
> Dad father = new Dad;
> Family son = new Boy;
> father.greeting;
> son.greeting;
> }
>
> The critical point is using a variable of type Family for an
> instance of Boy. Class Family covers the greeting method of
> Boy. In real OOP that would not be a problem, because the
> access point of view starts with the object. In D, it starts
> with the class definition.
>
> Is there any way to get real OOP with D?
>
> Regards, Ozan
Can you name an OOP oriented language that allows this ? Your
example is eroneous OOP.
The 2 other answers you 've got (the first using an interface and
the second using an abstract class) are valid OOP.
One of the fundamental concept OOP is that a function defined in
a class exists also in its subclasses. So how do you expect
`greeting()` to exist in Family if it's only defined in its
sub-classes ?
You can verify that with the 'Liskov substitution principle'
(https://en.wikipedia.org/wiki/Liskov_substitution_principle).
Actually your sample violates this principle.
More information about the Digitalmars-d-learn
mailing list