by CSMastermind · 3y ago · 0 comments
In C# I could easily write:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public class Employee : Person // Employee inherits from Person
{
public string Company { get; set; }
}
Whereas in Go I might have to do this for "data embedding": type Person struct {
Name string
Age int
}
type Employee struct {
Person // This is embedding Person struct in Employee
Company string
}
There's nothing stopping me from doing that in C#. public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public class Employee
{
public Person person { get; set; }
public string Company { get; set; }
}
> I do think it's important for a programming language to separate these aspects from classical inheritance itself...they are already separated?
interface IPerson {
public string Name { get; set; }
public int Age { get; set; }
}
public class Person : IPerson
{
public string Name { get; set; }
public int Age { get; set; }
}
public class Employee : IPerson
{
private Person _person;
public string Name {
get { return _person.Name; }
set { _person.Name = value; }
}
public int Age {
get { return _person.Age; }
set { _person.Age = value; }
}
public string Company { get; set; }
}
Employee conforms to the same interfaces as Person, but achieves this via delegation (the famous "favour composition over inheritance" thing)Please, note I don't dispute the "prefer composition over inheritance" thing. In fact, I heartily agree with it.
But in this specific case, where every sinle detail of Person is publicly exposed in Employee (no pun intended), I don't see a material difference. If anything, the example uses composition as a tool to implement inheritance on top of.
Am I missing something?
* https://wiki.c2.com/?AlanKaysDefinitionOfObjectOriented
* https://wiki.c2.com/?AlanKayOnMessaging
"OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme LateBinding of all things." ~ Alan Kay
"The key in making great and growable systems is much more to design how its modules communicate rather than what their internal properties and behaviors should be." ~ Alan Kay
The antithesis of OOP is exposing the name and age of a Person to the greater system at large.
That definition is dead. Wish it well, then bid it farewell. "OOP" means "Java" and we can just call Erlang and co "Agent Model".