This question is NOT question like "inheritence vs composition".
I understand completely how inheritance differs from composition, I know the Liskov substitution principle, the diamond problem, advantages and disadvantages both of them and both concepts seem to be simple. But there is so many questions everywhere about inheritance and composition, that i thought, maybe I misunderstand something in this simple idea.
Lets focus on Go. Go is a language from Google and everybody is excited it has no inheritance, it has no classes, but it has composition and this is cool. For me, the composition in Go gives you exactly the same functionality as inheritance in other languages (C++, Java, ...) - component methods are automatically exposed and available as methods of later structs, like here:
package main
import (
"fmt"
)
type Car struct{
name string
}
func (c *Car) move() bool {
return true
}
type MyCar struct{
Car
}
func main() {
var c MyCar
fmt.Print(c.move())
}
So to sum everything up, composition is better than inheritance because:
- is more flexible (allows you to change the component in runtime, so you can affect the way "classes" work.
- is free from diamond problem (but diamond problem is solvable, so this is not strong advantage)
And if you consider Go and its interfaces (every object, that has methods defined by an interface, implements this interface implicite) do you have the ultimate solution? Can we say that composition with some syntactic sugar can replace inheritance?
Such design agrees with Liskov substitution principle. Do I miss something or inheritance (known from any language) has no advantages over composition (and interfaces) known from Go?
===== edit1 =====
For clarification, it is possible in Go
to use "standard" composition mechanism, like this (this example behaves like the previous one):
package main
import (
"fmt"
)
type Car struct{
name string
}
func (c *Car) move() bool {
return true
}
type MyCar struct{
car Car
}
func (c *MyCar) move() bool {
return c.car.move()
}
func main() {
var c MyCar
fmt.Print(c.move())
}
But if you use it like in the previous example, all the methods are available implicite "in MyCar class".
var c MyCar; c.Car.move(); /*or the same */ c.move()
. So if this is the same as inheritance to you, then the answer to my question is "yes, you can use composition with some syntactic sugar to replace inheritance" – DefileMyCar
as if it is an instance ofCar
. If you passMyCar
instance to a function acceptingCar
, you get an error. I understeand all that conversations and I was wondering if we can replace both use cases with a single mechanism :) – Defile