Composition And Inheritance

Print   

02 Nov 2017

Use this example to study how a writer organises ideas, uses evidence and develops an argument. Write your own response and cite sources according to your institution’s guidance.

Object Composition and Inheritance are two techniques for reusing functionality in Object Oriented Systems. It is a fundamental activity of Software System Design to establish relation between two classes. They are Composition and Inheritance. There are two kinds of relationships, One is "is a" represents Inheritance, "has a" represents composition. A Class can have references to Objects of other classes as members in composition. Composition is better way to use where inheritance should not be used. For example if we inspired by someone we adobe the properties of that person and we have to add the quality of the person i.e. composition. But some quality of our parent automatically in us there is no need to strive i.e. inheritance. Composition is implemented simply by forwarding all calls to an Object Field. Composition more flexible, It is defined dynamically and not at compile time. Composition is late binding, we can change component dynamically. Object composition is defined at runtime, Object acquiring references to other objects. In such cases these objects will never be able to reach other protected data.There is ' no encapsulation break' in Object Composition. Inheritance is reuse technique, Object composition applied again and again in the design patterns.Composite is responsible for allocation and deallocation memory for an Object. In general, object composition should be favored over inheritance. It promotes smaller, more focused classes and smaller inheritance hierarchies.

Composition is easy to change behavior on dependency injection setters and getters. Composition is favored over Inheritance. Because in Java we can't extends more than one class. Composition also removes the ambiguity in other OOPs languages. Both Inheritance and Composition are different, In Composition we can refer some other class who hold the same properties; we can't reuse the code directly. In Inheritance we can inherit all super class properties into a sub class. So we can call super class properties from sub class Object. Inheritance is more rigid as most languages don't allow deriving from more than one class. For example B Class wants to expose the complete interface of Class A, such that Class B can be used wherever class A is expected, is called Inheritance. Class B want only some part of the behavior class A, indicates need for Composition. Composition is typically has a relationship, for ex: Person has Name. Inheritance uses is a relationship, for ex: Person is mammal.

Inheritance and limitations

It creates a strong relationship between two classes. Inheritance is also known as constructing a new class with already existed class features. Sharing the properties from one class (super) to another class (sub class) Java use extends keyword to provide inheritance between two classes. We can access the all super class properties from sub class Object. Inheritance creates a strong relationship between sub class and super class. Sub class must be aware of super class implementation details. Inheritance is useful when hierarchy represents is a relationship, and should be closed for modification. Inheritance creates an additional complexity, and more overused. In Inheritance we can't change the implementation at runtime, because inheritance defined at compilation time. Inheritance breaks encapsulation, because sub class exposes its super class implementation details. Inheritance is tight coupled, if any change in super class implementation will force to sub class to change.