Difference between Inheritance and Composition

前端 未结 17 1921
忘了有多久
忘了有多久 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:08

    Though both Inheritance and Composition provides code reusablility, main difference between Composition and Inheritance in Java is that Composition allows reuse of code without extending it but for Inheritance you must extend the class for any reuse of code or functionality. Another difference which comes from this fact is that by using Composition you can reuse code for even final class which is not extensible but Inheritance cannot reuse code in such cases. Also by using Composition you can reuse code from many classes as they are declared as just a member variable, but with Inheritance you can reuse code form just one class because in Java you can only extend one class, because multiple Inheritance is not supported in Java. You can do this in C++ though because there one class can extend more than one class. BTW, You should always prefer Composition over Inheritance in Java, its not just me but even Joshua Bloch has suggested in his book

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

    Inheritance between two classes, where one class extends another class establishes "IS A" relationship.

    Composition on the other end contains an instance of another class in your class establishes "Has A" relationship. Composition in java is is useful since it technically facilitates multiple inheritance.

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

    No , Both are different . Composition follow "HAS-A" relationship and inheritance follow "IS-A" relationship . Best Example for composition was Strategic pattern .

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

    Inheritence means reusing the complete functionality of a class, Here my class have to use all the methods of the super class and my class will be titely coupled with the super class and code will be duplicated in both the classes in case of inheritence.

    But we can overcome from all these problem when we use composition to talk with another class . composition is declaring an attribute of another class into my class to which we want to talk. and what functionality we want from that class we can get by using that attribute.

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

    Inheritance brings out IS-A relation. Composition brings out HAS-A relation. Strategy pattern explain that Composition should be used in cases where there are families of algorithms defining a particular behaviour.
    Classic example being of a duck class which implements a flying behaviour.

    public interface Flyable{
     public void fly();
    }
    
    public class Duck {
     Flyable fly;
    
     public Duck(){
      fly = new BackwardFlying();
     }
    }
    

    Thus we can have multiple classes which implement flying eg:

    public class BackwardFlying implements Flyable{
      public void fly(){
        Systemout.println("Flies backward ");
      }
    }
    public class FastFlying implements Flyable{
      public void fly(){
        Systemout.println("Flies 100 miles/sec");
      }
    }
    

    Had it been for inheritance, we would have two different classes of birds which implement the fly function over and over again. So inheritance and composition are completely different.

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

    The answer given by @Michael Rodrigues is not correct (I apologize; I'm not able to comment directly), and could lead to some confusion.

    Interface implementation is a form of inheritance... when you implement an interface, you're not only inheriting all the constants, you are committing your object to be of the type specified by the interface; it's still an "is-a" relationship. If a car implements Fillable, the car "is-a" Fillable, and can be used in your code wherever you would use a Fillable.

    Composition is fundamentally different from inheritance. When you use composition, you are (as the other answers note) making a "has-a" relationship between two objects, as opposed to the "is-a" relationship that you make when you use inheritance.

    So, from the car examples in the other questions, if I wanted to say that a car "has-a" gas tank, I would use composition, as follows:

    public class Car {
    
    private GasTank myCarsGasTank;
    
    }
    

    Hopefully that clears up any misunderstanding.

    0 讨论(0)
提交回复
热议问题