This is fairly basic topic in OOAD but I have seen most of programmers do not understand subtleties .
Mostly they do not want to think hard and make inheritance as default choice of implementation for re-use .
Finally end up with BIG FAT parent class extended by many classes with lot of if-else conditions and only god know what is the mess in that class .
Inheritance is achieved by extending class or interface/interfaces in java .
Composition is achieved by building stronger HAS_A relationship .
Inheritance (Compile Time Structure)
1. Uses sub-classing or extending the base class (interface or class).
2. It is called white-box re-use because it knows internals of parent class.
3. New functionality is achieved by partially or completely re-defining of parent functionality .
4. Defined statically at compile time and easy to understand.
5. Inheritance break encapsulation - because it has access to internal state of parent class . If some thing is changed at parent class we need re-test all extended classes also . It might cause ripple effect of change if you change anything in parent class to all sub-classes.
6. To use inheritance you should know all internal state and documentation of parent class so there is learning curve .
7. Relationships can not be re-defined at run time .
8. Has to deal with small number of classes.
Composition (Run Time Structure)
1. Uses composition of small objects which have well defined interfaces .
2.It is called black-box re-use because object using the object do not know internal details of the object .
3. New functionality is developed by using small functionality of number of objects.
4. Composition have well defined interfaces to all small objects so as long as there interface is same user object does not care details .
5. Composition promotes encapsulation allowing to change parent and subclass to change and evolve independently .
6. To use composition you do not need to know internal state of objects and once you know interfaces there is very small learning curve so code is easy to understand .
7. As per need you can compose object with large number of small objects to mimic required behavior .
8. Has to deal with large number of small classes.
Mostly they do not want to think hard and make inheritance as default choice of implementation for re-use .
Finally end up with BIG FAT parent class extended by many classes with lot of if-else conditions and only god know what is the mess in that class .
Inheritance is achieved by extending class or interface/interfaces in java .
Composition is achieved by building stronger HAS_A relationship .
Inheritance (Compile Time Structure)
1. Uses sub-classing or extending the base class (interface or class).
2. It is called white-box re-use because it knows internals of parent class.
3. New functionality is achieved by partially or completely re-defining of parent functionality .
4. Defined statically at compile time and easy to understand.
5. Inheritance break encapsulation - because it has access to internal state of parent class . If some thing is changed at parent class we need re-test all extended classes also . It might cause ripple effect of change if you change anything in parent class to all sub-classes.
6. To use inheritance you should know all internal state and documentation of parent class so there is learning curve .
7. Relationships can not be re-defined at run time .
8. Has to deal with small number of classes.
Composition (Run Time Structure)
1. Uses composition of small objects which have well defined interfaces .
2.It is called black-box re-use because object using the object do not know internal details of the object .
3. New functionality is developed by using small functionality of number of objects.
4. Composition have well defined interfaces to all small objects so as long as there interface is same user object does not care details .
5. Composition promotes encapsulation allowing to change parent and subclass to change and evolve independently .
6. To use composition you do not need to know internal state of objects and once you know interfaces there is very small learning curve so code is easy to understand .
7. As per need you can compose object with large number of small objects to mimic required behavior .
8. Has to deal with large number of small classes.
So suppose you have small N number of small objects with specific focused behavior ideally you should able to compose 2^n possible compositions .
So Remember favor object composition over class inheritance for more testable code , less learning curve and more encapsulation so reuse .
No comments:
Post a Comment