Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The point is that the Golang version is closer to this:

      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)


What advantages does the Golang version have over inheritance?

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?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: