Sunteți pe pagina 1din 2

Inheritance vs Containership

Inheritance and Containership are two important concepts found


in OOP (Object Oriented Programming Example: C++). In simple
terms, both Containership and Inheritance deal with providing
additional properties or behavior to a class. Inheritance is the
ability for a class to inherit properties and behavior from a parent
class by extending it. On the other hand, Containership is the
ability of a class to contain objects of different classes as member
data.
What is Inheritance?
As mentioned above, Inheritance is the ability for a class to inherit
properties and behavior from a parent class by extending it.
Inheritance essentially provides code reuse by allowing extending
properties and behavior of an existing class by a newly defined
class. If class A extends B, then class B is called the parent class
(or super class) and class A is called the child class (or derived
class/sub class). In this example scenario, class A will inherit all
public and protected attributes and methods of the super class
(B). The subclass can optionally override (provide new or
extended functionality to methods) the behavior inherited from the
parent class. Inheritance represents an is-a relationship in OOP.
This essentially means that A is also a B. In other words, B can be
the class with a general description of a certain real world entity
but A specifies a certain specialization. In a real world
programming problem, the Person class could be extended to
create the Employee class. This is called specialization. But you
could also first create the Employee class and then generalize it
to a Person class as well (i.e. generalization). In this example, the
Employee will have all the properties and behavior of the Person
(i.e. Employee is also a Person) and may contain some additional
functionality (so, Person is not an Employee) as well.
What is Containership?

Containership is the ability of a class to contain objects of different


classes as member data. For example, class A could contain an
object of class B as a member. Here, all the public methods (or
functions) defined in B can be executed within the class A. Class
A becomes the container, while class B becomes the contained
class. Containership is also referred to as Composition. In this
example, it can be said that class A is composed of class B. In
OOP, Containership represents a has-a relationship. It is
important to note that, even though the container has access to
execute all the public methods of the contained class, it is not
able to alter or provide additional functionality. When it comes to a
real world programming problem, an object of class TextBox may
be contained in the class Form, and thus can be said that a Form
contains a TextBox (or alternatively, a Form is composed of a
TextBox).
Difference between Inheritance and Containership
Although Inheritance and Containership are two OOP concepts,
they are quite different in what they allow the programmer to
achieve. Inheritance is the ability for a class to inherit properties
and behavior from a parent class by extending it, while
Containership is the ability of a class to contain objects of different
classes as member data. If a class is extended, it inherits all the
public and protected properties/behavior and those behaviors
may be overridden by the subclass. But if a class is contained in
another, the container does not get the ability to change or add
behavior to the contained. Inheritance represents an is-a
relationship in OOP, while Containership represents a has-a
relationship.

S-ar putea să vă placă și