Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Inheritance VS Composition

Introduction

The relationship among classes is one of the fundamental activities of software design.
There are two ways to relate classes: inheritance and composition.

Inheritance

Inheritance enables us to create a new class that reuses and extends behavior from
another class called a base class or superclass and the newly created class is called the
derived class. When we inherit a class from any other class, the derived class implicitly
gains all the members of the base class except the constructors and destructor. So, the
derived class can reuse a base class method and property without re-implementing it.
In other words we can say, using inheritance, a derived class extends the base class
functionality. The main advantage of inheritance is the reusability of the code. The other
main advantages of class inheritance are, it makes it easy to modify the current
implementation of the base class by just overriding an operation within the derived
class. The limitation with inheritance is, we cannot change the implementation of the
base class at run time.

Inheritance is a hierarchical relationship between classes. Inheritance can denote a "is -


a" relationship between classes.

Inheritance is sometimes said to provide "weak encapsulation," because if you have


code that directly uses a derived class then that code can be broken by changes to a
base class.

An inheritance relationship between base classes and derived classes is often said to be
fragile, because small changes done in the base class can ripple out and might require
changes in many places within an application.

Example of Inheritance

public class BaseClass


{
public string property { get; set; }

public void BaseMethod()


{
// Do something....
}
}

public class DerivedClass : BaseClass


{
public string DerivedclassProperty { get; set; }
public void DerivedMethod()
{
// Do something....
base.BaseMethod();
}
}

In this example DerivedClass is related to the BaseClass by inheritance, here BaseClass


is a superclass and DerivedClass is the subclass.

Composition

Composition is the most commonly used relation in a class diagram. In general terms,
composition allows a class to contain an object instance of another class. Composition
can be denoted as being an "as a part" or a "has a" relationship between classes.

In the composition approach, the derived class becomes the front-end class and the
base class becomes the back-end class. The composition approach provides stronger
encapsulation than inheritance, because a change to a back-end class does not
necessarily break any code that relies on the front-end class. The main advantages of
composition is, with carefully designed interfaces we can change references of back end
classes at runtime.

Example of composition

public class FirstClass


{
public string property { get; set; }

public void FirstMethod()


{
// Do something....
}
}

public class SecondClass


{
private FirstClass firstClass = new FirstClass();
public string ClassProperty { get; set; }

public void MyMethod()


{
// Do something....
firstClass.FirstMethod();
}
}

In this example SecondClass (also called the front end class) is related to FirstClass (also
called the back end class) by composition. SecondClass has an instance variable that
holds a reference to a FirstClass object. In the composition, the front-end class holds the
reference of the back end class as an instance variable.

Inheritance VS Composition

Inheritance Composition

In inheritance, there is an image of the base


Composition allows late creation of the backend
class in the derived class object, so the image
class object until and unless they are not really
of the base class is created when the derived
required.
class object is created.

In composition, life of the backend class is


Base class remains a part of the derived class
independent and we can change back end object
object throughout the life of the derived class.
dynamically.

Inheritance is static binding (compile time


Composition is dynamic binding (run time binding)
binding)

Inheritance can denote an "is - a" relationship Composition can be denoted as being an "as a
between classes. part" or "has a" relationship between classes.

Inheritance comes with polymorphism. NA

In inheritance, there is a single invocation of The explicit method invocation (forwarding or


an inherited base class so there is no extra delegation) has a performance cost (note that
cost for invocation. performance depends on many factors).

A change in the base class interface cannot


It is easy to change the interface of a back-end
ripple down the inheritance hierarchy to a
class and front-end class
derived class.
In inheritance, it is not required to implement In composition, all methods provided by
all base class methods within the derived composed classes must be implemented in the
class. front end class.

The derived class and base class interfaces are The front-end and back-end interfaces are loosely
tightly coupled. coupled.

It is easier to add new derived classes in inheritance than to add new front-end class in
composition.

Conclusion

The inheritance model is denoted as being an "is - a" relationship and composition is
denoted as being a "has - a" relation ship between classes. The developer is always
confused, choosing between inheritance and composition. Do not use inheritance just
for code reuse. If there is no is-a relationship between classes then use composition. Do
not use inheritance to get polymorphism. We can use inheritance to express differences
in behavior and fields to express variations in state in a state design pattern.
IsA and HasA Relationship in C#
In this article, I am going to discuss IsA and HasA Relationships in C# with Examples.
Please read our Inheritance in C# article before proceeding to this article. It is very
important to understand the concept of IS-A and HAS-A relationship between classes.
Many students, many developers, especially beginners and freshers get confused
between the IS-A and HAS-A relationship and because of this, they are making the
relationship between classes in the wrong order. So, today, I will explain what is exactly
IS-A and HAS-A relationships, what is the differences between them, and when to use
and how to use IS-A and HAS-A relationships in real-time application. Here, I am going
to show the examples using the C# language but this is also applicable to any other
object-oriented programming language Java, C++, etc.
IsA and HasA Relationship in C# with Examples:
Let us understand IS-A and HAS-A Relationship in C# with one example. Please have a
look at the below example.

class Rectangle{
Some Data Members
Some Member Functions
}
AD

This is a class called Rectangle. Let us assume data we have some data members and
member functions there inside this class. Next, we have a class called Cuboid which is
inheriting from the above Rectangle class as follows.

class Cuboid : Rectangle{


Some Data Members
Some Member Functions
}
AD

This is the Cuboid class which is inherited from the Rectangle class. Also, assume that
this class also has some data members and member functions inside it. Now let us write
one more class as follows.

class Table{
Rectangle top;
int legs;
}

This is the Table class which is not inherited from any class. This class has two data
members. One of the data members is of class Rectangle and the other is of integer
type variable i.e. top and legs.
AD

So first we have created a class called Rectangle. Then we created another class called
Cuboid which is inherited from the Rectangle class and then we created another class
called Table. Inside the Table class, we have created one variable of the type Rectangle
as well as another variable of integer type.
The class Cuboid is inherited from the Rectangle class. So, can we say that a Cuboid IA
A Rectangle? Yes. So, the relationship between the Rectangle class and Cuboid class is
the Is A relationship. So, the point that you need to remember is whenever we are
making the Inheritance relationship or Parent-Child relationship, then we can say that
relationship as IS-A relationship in object-oriented programming languages.
Next, our Table class, it is having a top variable that is of the type Rectangular. That
means the Table class is having an object of the Rectangle class. So, can we say that
the Table class HAS A Rectangle? Yes, the Table class Has a Rectangle. So, the
relationship between the Table class and the Rectangle class is the Has A relationship.
So, we can use our class in two ways that are ‘Is A’ and ‘Has A’. This is common in
object-oriented programming languages just not for C#, it is available in C++, Java, and
other Object-Oriented Programming Language.
So, a class in C# can be used in two ways. One way is by using the Inheritance
relationship i.e. one class is inheriting from that class. The second approach is that you
can create an object of that class and use it. So, there are two ways of using one class.
Either you can create the object and use it or you can inherit it from that class.

How to Decide What to Implement between IS-A and HAS-A?


Simply, ask the question yourself. For example, If I ask you the questions which
statement gives you more sense from the below two statements?

Employee IS-A Address


Employee HAS-A Address

Then definitely, you will tell Employee HAS-A Address gives more sense than Employee
IS-A Address. Now, again If I ask you, which statement gives you a better sense of the
below two statements?

BMW IS-A Car


BMW HAS-A Car

Then definitely your answer will be BMW IS-A Car.

Note: In C#, the IS-A relationship is implemented using Inheritance and the HAS-A
relationship is implemented using Composition i.e. declaring a variable. So, whenever
we declare a variable of one class inside another class, we called it a Composition or you
can say HAS-A relationship.

You might also like