Difference between Inheritance and Composition

前端 未结 17 1848
忘了有多久
忘了有多久 2020-11-22 02:35

Are Composition and Inheritance the same? If I want to implement the composition pattern, how can I do that in Java?

相关标签:
17条回答
  • 2020-11-22 03:26

    as another example, consider a car class, this would be a good use of composition, a car would "have" an engine, a transmission, tires, seats, etc. It would not extend any of those classes.

    0 讨论(0)
  • 2020-11-22 03:27

    They are absolutely different. Inheritance is an "is-a" relationship. Composition is a "has-a".

    You do composition by having an instance of another class C as a field of your class, instead of extending C. A good example where composition would've been a lot better than inheritance is java.util.Stack, which currently extends java.util.Vector. This is now considered a blunder. A stack "is-NOT-a" vector; you should not be allowed to insert and remove elements arbitrarily. It should've been composition instead.

    Unfortunately it's too late to rectify this design mistake, since changing the inheritance hierarchy now would break compatibility with existing code. Had Stack used composition instead of inheritance, it can always be modified to use another data structure without violating the API.

    I highly recommend Josh Bloch's book Effective Java 2nd Edition

    • Item 16: Favor composition over inheritance
    • Item 17: Design and document for inheritance or else prohibit it

    Good object-oriented design is not about liberally extending existing classes. Your first instinct should be to compose instead.


    See also:

    • Composition versus Inheritance: A Comparative Look at Two Fundamental Ways to Relate Classes
    0 讨论(0)
  • 2020-11-22 03:27

    I think this example explains clearly the differences between inheritance and composition.

    In this exmple, the problem is solved using inheritance and composition. The author pays attention to the fact that ; in inheritance, a change in superclass might cause problems in derived class, that inherit it.

    There you can also see the difference in representation when you use a UML for inheritance or composition.

    http://www.javaworld.com/article/2076814/core-java/inheritance-versus-composition--which-one-should-you-choose-.html

    0 讨论(0)
  • 2020-11-22 03:33

    Composition is where something is made up of distinct parts and it has a strong relationship with those parts. If the main part dies so do the others, they cannot have a life of their own. A rough example is the human body. Take out the heart and all the other parts die away.

    Inheritance is where you just take something that already exists and use it. There is no strong relationship. A person could inherit his fathers estate but he can do without it.

    I don't know Java so I cannot provide an example but I can provide an explanation of the concepts.

    0 讨论(0)
  • 2020-11-22 03:33

    Inheritances Vs Composition.

    Inheritances and composition both are used to re-usability and extension of class behavior.

    Inheritances mainly use in a family algorithm programming model such as IS-A relation type means similar kind of object. Example.

    1. Duster is a Car
    2. Safari is a Car

    These are belongs to Car family.

    Composition represents HAS-A relationship Type.It shows the ability of an object such as Duster has Five Gears , Safari has four Gears etc. Whenever we need to extend the ability of an existing class then use composition.Example we need to add one more gear in Duster object then we have to create one more gear object and compose it to the duster object.

    We should not make the changes in base class until/unless all the derived classes needed those functionality.For this scenario we should use Composition.Such as

    class A Derived by Class B

    Class A Derived by Class C

    Class A Derived by Class D.

    When we add any functionality in class A then it is available to all sub classes even when Class C and D don't required those functionality.For this scenario we need to create a separate class for those functionality and compose it to the required class(here is class B).

    Below is the example:

              // This is a base class
                     public abstract class Car
                        {
                           //Define prototype
                           public abstract void color();
                           public void Gear() {
                               Console.WriteLine("Car has a four Gear");
                           }
                        }
    
    
               // Here is the use of inheritence
               // This Desire class have four gears.
              //  But we need to add one more gear that is Neutral gear.
    
              public class Desire : Car
                       {
                           Neutral obj = null;
                           public Desire()
                           {
         // Here we are incorporating neutral gear(It is the use of composition). 
         // Now this class would have five gear. 
    
                               obj = new Neutral();
                               obj.NeutralGear();
                           }
    
                           public override void color()
                           {
                               Console.WriteLine("This is a white color car");
                           }
    
                       }
    
    
                 // This Safari class have four gears and it is not required the neutral
                 //  gear and hence we don't need to compose here.
    
                       public class Safari :Car{
                           public Safari()
                           { }
    
                           public override void color()
                           {
                               Console.WriteLine("This is a red color car");
                           }
    
    
                       }
    
       // This class represents the neutral gear and it would be used as a composition.
    
       public class Neutral {
                          public void NeutralGear() {
                               Console.WriteLine("This is a Neutral Gear");
                           }
                       }
    
    0 讨论(0)
提交回复
热议问题