Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>The Short Answer</h2> <p>It's really not as black and white as that. In short, yes. Any situation that can be solved with inheritance can be solved, near enough, by composition. So in short, the answer to your question is yes; inheritance can be replaced by composition.</p> <h2>Why it's not that simple</h2> <p><b> When to use Inheritance </b><br> It's not a matter of whether you CAN swap them out. It depends on the context that you're programming in, and it becomes more of a question of whether you SHOULD swap them out. Take this simple example in Java:</p> <pre><code>public class Person { // Assume every person can speak. public void speak() { } } </code></pre> <p>Now, let's say we have another class, Dave. Dave IS a person.</p> <pre><code>public class Dave extends Person { public void speak() { System.out.println("Hello!"); } public void killSomeone() {} // Dave is a violent Guy. } </code></pre> <p>Now would it make more sense for the class <code>Dave</code> to look like this?</p> <pre><code>public class Dave { private Person p; // Composition variant. public void speak() { p.speak(); } public void killSomeone() {} // Dave is a violent Guy. } </code></pre> <p>This code implies Dave has a person. It's not as simple and doesn't explain itself as well. Also, anything a Person can do, Dave can do, so it makes sense that we assert Dave is a "Person".</p> <p><b> When to use Composition </b><br></p> <p>We use Composition when we only want to expose part of the class' interface. Following our previous example, let's say <code>Dave</code> has a <code>Guitar</code>. The guitar has a more complex interface:</p> <pre><code>public class Guitar { public Color color; // Guitar's color. public Tuning tuning; // Guitar's tuning. public void tuneGuitar() {} public void playChord() {} public void setColor() {} } </code></pre> <p>Now, if we were to inherit this class, what would the outcome be?</p> <p>Well, class Dave would now have attributes <code>color</code> and <code>tuning</code>. Does <code>Dave</code> have a tuning? I think not! This is where inheritance makes no sense. We don't want to expose the entire <code>Guitar</code> interface along with the <code>Dave</code> interface. We only want the user to be able to access what <code>Dave</code> needs to access, so in this case we would use some composition:</p> <pre><code>public class Dave extends Person { private Guitar guitar; // Hide the guitar object. Then limit what the user can do with it. public void changeGuitarColor(Color newColor) { // So this code makes a lot more sense than if we had used inheritance. guitar.setColor(newColor); } public void speak() { System.out.println("Hello!"); } public void killSomeone() {} // Dave is a violent Guy. } </code></pre> <h2>Conclusion</h2> <p>It's really not a case of what can replace the other. It's about the situation that you are implementing the techniques in. Hopefully, by the end of the example you'll see that inheritance is for situations where one object <code>IS A</code> object, and composition is used when one object <code>HAS A</code> object.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. COChris I very appreciate your answer, but your answer is language syntax specyfic - please see my example. I understand *is a* and *has*, but using some special syntax, like in Go, we can mimic both behaviors with composition. You can get used to the Go syntax and see that MyCar **is** a Car if Car is in list of its "components" - but this is only syntax and apperance - it could be improoven. We can use in `Go` "normal" composition, as you have shown here it the components are named (in my example, the `Car` is name of a type and it is called an `unnamed component` or `unnamed variable`)
      singulars
    2. COI simply used Java as an example, because it's the language I am most familiar with and I knew I could explain the concept better with it. I'm sorry, I don't completely understand your comment.
      singulars
    3. COPlease look at edited question and additional example. Sorry for being not clear to you. I was trying to say, that you are talking about apperance of things (something looks like or something is like something else). This apperance is strongly connected with syntax. I dont want to talk about specific language (this should be language agnostic question). In Go inheritance is replaced by composition and special syntax allowing promoting methods of the components. I wanted to know if it is a "silver bullet" or it is weaker in some cases than standard inheritance.
      singulars
 

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