Note that there are some explanatory texts on larger screens.

plurals
  1. POCan inheritance be replaced completely by composition?
    primarykey
    data
    text
    <p>This question is <strong>NOT</strong> question like "inheritence vs composition".</p> <p>I understand completely how inheritance differs from composition, I know the <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle" rel="noreferrer">Liskov substitution principle</a>, the <a href="http://en.wikipedia.org/wiki/Multiple_inheritance" rel="noreferrer">diamond problem</a>, 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.</p> <p>Lets focus on <a href="http://golang.org/" rel="noreferrer">Go</a>. 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 <strong>exactly</strong> the same functionality as inheritance in other languages (C++, Java, ...) - component methods are automatically exposed and available as methods of later structs, like here:</p> <pre class="lang-go prettyprint-override"><code>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()) } </code></pre> <p>So to sum everything up, composition is better than inheritance because:</p> <ol> <li>is more flexible (allows you to change the component in runtime, so you can affect the way "classes" work.</li> <li>is free from diamond problem (but diamond problem is solvable, so this is not strong advantage)</li> </ol> <p><strong>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?</strong></p> <p>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?</p> <p><strong>===== edit1 =====</strong></p> <p>For clarification, it is possible in <code>Go</code> to use "standard" composition mechanism, like this (this example behaves like the previous one):</p> <pre><code>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()) } </code></pre> <p>But if you use it like in the previous example, all the methods are available implicite "in MyCar class".</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload