Compare the Difference Between Similar Terms

Difference Between

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

Difference Between Inheritance and Composition

July 10, 2011Posted byIndika

Inheritance vs Composition

Inheritance and Composition are two important concepts found in OOP (Object Oriented Programming). In simple terms, both Composition 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, Composition 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.

继承了一个“是”的关系OP. 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 Composition?

Composition 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. Composition is also referred to as Containership. In this example, it can be said that class A is composed of class B. In OOP, Composition 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).

What is the difference between Inheritance and Composition?

Although Inheritance and Composition 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 Composition 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 Composition represents a “has-a” relationship.

Related posts:

Difference Between Inheritance and Containership Difference Between Structured Programming and Object Oriented Programming Difference Between AOP and OOP Difference Between Object Oriented Programming and Procedural Programming Difference Between Object and Instance

Filed Under:ProgrammingTagged With:Composition,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 ReplyCancel 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 Artifact and Fossil

Difference Between Artifact and Fossil

Difference Between Latimer Diagram and Frost Diagram

Difference Between Latimer Diagram and Frost Diagram

Difference Between Ventilation and Respiration

Difference Between Diapsid and Synapsid

Difference Between Diapsid and Synapsid

Difference Between Lectin and Lecithin

Difference Between Lectin and Lecithin

Latest Posts

  • What is the Difference Between Aquagenic Urticaria and Aquagenic Pruritus
  • What is the Difference Between Astringent and Toner
  • 食管炎之间的区别是什么nd Barrett’s Esophagus
  • What is the Difference Between Alcohol Ink and Resin Dye
  • What is the Difference Between Hyperparathyroidism and Hyperthyroidism
  • What is the Difference Between Pearlescent and Iridescent
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

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