Difference Between Inheritance and Containership
Inheritance and Containership two important concepts found in OOP (Object Orientated Programming: Example- C++). In simple terms, both Containership and Inheritance is all about providing additional properties or behavior to a class. Inheritance is the capacity for a class to inherit properties and behavior of a parental class by spreading it. On the other hand, Containership is the capacity of a class to contain objects of different classes as member’s data.
What is Inheritance?
As mentioned above, Inheritance is the capacity for a class to inherit properties and behavior of a parental class by spreading it. Inheritance principally provides code reuse by allowing extending properties and behavior of an existing class by a newly defined class. If the class A spreads B, therefore they call the class B the parental class (or the super class) and the class A is called the child class (or derived class/sub class). In this example scenario, the class A will inherit all public and protected attributes and methods of the super class (B). The subclass can optionally override (provide the new or extended functionality in methods) the behavior inherited from the parental class. Inheritance represents one ‘ is -a ‘ relation in OOP. It means principally that A is also a B. In other words, B can be the class with a general description of a certain entity of the real world but A specifies a certain specialization. In the real world programming problem, the Person class could be extended to create the Employee class. They call it specialization. But you could also first create the Employee class and then make general to a Person class too (that is to say general implementation). In this example, the Employee will have all ownership and behavior of the Person (that is to say. The employee is also a Person) and can contain a little of additional functionality (so, the Person is not an Employee) also.
What is Containership?
Containership is the capacity of a class to contain objects of different classes as member data. For example, the class A could contain an object of class B as a member. Here, all public methods (or functions) defined in B can be carried out in the class A. The Class A becomes the container, while the class B becomes the contained class. Containership is so called Composition. In this example, they can say it that the class A is composed of the class B. In OOP, Containership represents one ‘ has – a’ relation. It is important to note that, although the container has access to carry out all public methods of the contained class, it is not able to change or provide additional functionality. When it comes to the real world programming problem, an object of class TextBox can be contained in the class Form and can so be said as a Form contains a TextBox (or in another way, a Form is composed of a TextBox).
Difference between Inheritance and Containership
Although Inheritance and Containership are two concepts of OOP, they completely differ in what they allow to the programmer to fulfil. Inheritance is the capacity for a class to inherit properties and behaviour from a parental class by spreading it, while Containership is the capacity of a class to contain objects of different classes as member’s data. If a class is extended, it inherits all public and protected properties/ behaviours and these behaviours can be overridden by the subclass. But if a class is contained in other one, the container does not get the capacity to change or add behaviour to the contained. Inheritance represents ‘is -a ‘ relationship in OOP, while Containership represents a’ has -a’ relationship.