Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming / Difference Between Inheritance and Containership

Difference Between Inheritance and Containership

May 12, 2011 Posted by Indika

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.

Related posts:

Difference Between Object Oriented Programming and Procedural Programming Difference Between Implements and Extends Difference Between Nationality and CitizenshipDifference Between Nationality and Citizenship Difference Between Flowchart and Data Flow Diagram (DFD) Difference Between Linear and Nonlinear Data StructuresDifference Between Linear and Nonlinear Data Structures

Filed Under: Programming Tagged With: Containership, Inheritance, Object Oriented Programming, OOP

About the Author: Indika

Indika, BSc.Eng, MSECE Computer Engineering, PhD. Computer Science, is an Assistant Professor and has research interests in the areas of Bioinformatics, Computational Biology, and Biomedical Natural Language Processing.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Roasting and Broiling

Difference Between Roasting and Broiling

Difference Between Alaskan Malamute and Alaskan Husky

Difference Between Hereafter and Henceforth

Difference Between Hereafter and Henceforth

Difference Between Schiff Base and Schiff’s Reagent

Difference Between Schiff Base and Schiff’s Reagent

Difference Between HTTP and FTP

Latest Posts

  • What is the Difference Between Diphtheria and Whooping Cough
  • What is the Difference Between Polyester and Satin
  • What is the Difference Between Syphilis and Gonorrhea
  • What is the Difference Between Stainless Steel and Sterling Silver
  • What is the Difference Between Infrared and Induction Cooker
  • What is the Difference Between Polyester and Viscose
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018 Difference Between. All rights reserved. Terms of Use and Privacy Policy: Legal.