Composition vs Inheritance
Composition :
Composition, simply mean using instance variables that are references to other objects. For example:
class A {
//...
}
class B {
private A objA = new A();
//...
}
Composition is a "has-a" relation.
Benefit of using composition is that we can control the visibility of other object to client classes and reuse only what we need.
Two fundamental ways to relate classes are inheritance and composition. Although the compiler and Java virtual machine (JVM) will do a lot of work for you when you use inheritance, you can also get at the functionality of inheritance when you use composition.
There are different views which favors COMPOSITION over Inheritance:
One example could be : java.util.Stack inherits java.util.Vector .
Stack is not a type of Vector, it should not be allowing random access, arbitrary insertion or deletion .
It could have been composition.
Another favor for composition is : java does not support multiple inheritance . SO you can extend only one class.
But to achieve the multiple functionality , you can make object of multiple class as member of your class or you can use interfaces. But inheritance will not work in this case.
For example your class wants to display the lyrics and play the song.
You can easily unit test a class by making a Mock object of that.
In the composition approach, the subclass becomes the "front-end class," and the superclass becomes the "back-end class." With inheritance, a subclass automatically inherits an implementation of any non-private superclass method that it doesn't override. With composition, by contrast, the front-end class must explicitly invoke a corresponding method in the back-end class from its own implementation of the method. This explicit call is sometimes called "forwarding" or "delegating" the method invocation to the back-end object.
The composition approach to code reuse provides stronger encapsulation than inheritance, because a change to a back-end class needn't break any code that relies only on the front-end class.
The composition approach to code reuse provides stronger encapsulation than inheritance, because a change to a back-end class needn't break any code that relies only on the front-end class.
We can't change the implementations inherited from parent classes at run-time, because inheritance is defined at compile-time.
Object composition is defined dynamically at run-time through objects acquiring references to other objects.
But when you establish an inheritance relationship between two classes, you get to take advantage of dynamic binding and polymorphism. Dynamic binding means the JVM will decide at runtime which method implementation to invoke based on the class of the object.
Composition :
Composition, simply mean using instance variables that are references to other objects. For example:
class A {
//...
}
class B {
private A objA = new A();
//...
}
Composition is a "has-a" relation.
Benefit of using composition is that we can control the visibility of other object to client classes and reuse only what we need.
Two fundamental ways to relate classes are inheritance and composition. Although the compiler and Java virtual machine (JVM) will do a lot of work for you when you use inheritance, you can also get at the functionality of inheritance when you use composition.
There are different views which favors COMPOSITION over Inheritance:
One example could be : java.util.Stack inherits java.util.Vector .
Stack is not a type of Vector, it should not be allowing random access, arbitrary insertion or deletion .
It could have been composition.
Another favor for composition is : java does not support multiple inheritance . SO you can extend only one class.
But to achieve the multiple functionality , you can make object of multiple class as member of your class or you can use interfaces. But inheritance will not work in this case.
For example your class wants to display the lyrics and play the song.
You can easily unit test a class by making a Mock object of that.
In the composition approach, the subclass becomes the "front-end class," and the superclass becomes the "back-end class." With inheritance, a subclass automatically inherits an implementation of any non-private superclass method that it doesn't override. With composition, by contrast, the front-end class must explicitly invoke a corresponding method in the back-end class from its own implementation of the method. This explicit call is sometimes called "forwarding" or "delegating" the method invocation to the back-end object.
The composition approach to code reuse provides stronger encapsulation than inheritance, because a change to a back-end class needn't break any code that relies only on the front-end class.
The composition approach to code reuse provides stronger encapsulation than inheritance, because a change to a back-end class needn't break any code that relies only on the front-end class.
We can't change the implementations inherited from parent classes at run-time, because inheritance is defined at compile-time.
Object composition is defined dynamically at run-time through objects acquiring references to other objects.
But when you establish an inheritance relationship between two classes, you get to take advantage of dynamic binding and polymorphism. Dynamic binding means the JVM will decide at runtime which method implementation to invoke based on the class of the object.
Comments
Post a Comment