Why Multiple Inheritance is discouraged?

OOPS-class-object
OOPS-class-object @Image courtesy of digitalart / FreeDigitalPhotos.net

Why Multiple Inheritance is discouraged?

Welcome to CodeSpread!

Hi Guys,
This is Sathish Again,Today we will discuss why most of the higher languages eliminates Multiple Inheritance?
Most of you guys already know that multiple inheritance is not available for development but what you might not know is the reason as why it is not possible. First of all,we will revisit few basics of inheritance.

What is Inheritance?

Wikipedia says “In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, or to establish a subtype from an existing object, or both, depending upon programming language support.”

In simple words, Inheritance forces reusability where a class reuses the functionality of another class by inheriting all its traits.We can take an example to understand more:

In real life, We inherit some of our behavior from our parents,so we can deduce our parents as Base class,and ourselves as Derived class.

What are the types of Inheritance?

Single Level Inheritance:

It is the basic type of Inheritance where child class B inherits/extends the behavior of a base class A.

For ex: We can have a child class like TV which extends base class like Electronics.

SingleInheritance

Multi Level Inheritance:

Here, Single inheritance is captured at different levels and called as multi-level inheritance. We have a derived class C which extends class B, which is a child class of base class A.

For ex: Can we say Ipad extends Laptop and Laptop extends Desktop?

image

Multiple Inheritance:

The concept of multiple inheritance is a derived class C extends more than one base class like class A and Class B. Multiple inheritance is not supported by many languages and can be achieved by using interfaces.

For ex: Child extends behavior of Father and Mother.

multiple inheritance

Now we will move to our basic question,
Consider the c++ program mentioned below , where I have created 3 classes,Two of them are base class and third one is the derived class,

  • Base1 and Base2 are Base classes
  • Derive is the Derived class

/*Program about multiple inheritance */

class Base1 {
public :
void Display() {
cout<<"We are in Base class1 " ;
}
} ;
class Base2 {
public :
void Display() {
cout<<"We are in Base class2 " ;
}
} ;

class Derive :public Base1,public Base2 {

} ;

If we focus on Base1 and Base2, then it is clearly visible that,

  • Base1 class has a method called Display
  • Base2 class also has a method called Display,

We extend these base classes with our derived class.

Let’s move forward and create an instance of derived class,


int main () {

Derive d;
d.Display();
return 0 ;
}

When we call the Display method,then the compiler won’t understand which Display method to call,whether from class A or from class B . This leads to ambiguity ,which is the reason why multiple inheritance is not supported in many higher languages.
But It can be achieved through interfaces that I will explain it on next Article,
For any queries, please comment over here or send it to admin@codespread.com

Author: Sathish

I just loved to express myself...

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

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

More from CodeSpread:

  • Concepts: S O L I DConcepts: S O L I DSOLID - Object oriented programming principles. May be its easy to forget these principles  but for creating a reusable code or maintainable code, people will always have to come back and check ...
  • Useful ‘ref’ and ‘out’ parametersUseful ‘ref’ and ‘out’ parameters It took me some time to realize the real potential of ref and out keywords, till the time I experience a condition which could only be simplified by implementing ref and out keyword. Lets st...
  • Asp.Net: More about CookiesAsp.Net: More about CookiesWe have seen, how our cookies looks in our last article Cookies Part 1. In this article, we will talk about their properties,limitations and technical part. Properties and Limitations Cook...
  • C#: My experience with const keywordC#: My experience with const keywordThere are numerous post on internet about 'what is const keyword in .net' or 'what is the difference between const keyword and read only' and I must say many might have already mugged it up like me...
  • Nullable .Net typesNullable .Net typesWe declare .net types in our project daily but still we do not recollect default values for each .net type so we end up doing null check for each data type. Its a basic mistake of a developer but ....
  • C# Code to send a MailC# Code to send a MailWhile working in a project, we always neglect the importance of the code which we have written many times and now end up in copy pasting the code from old projects. Lately, I saw a request in fo...
  • Asp.Net: Hidden Truth of CookiesAsp.Net: Hidden Truth of CookiesI know there are several articles on cookies as what are they? and how can they be created? but i always wanted to know more about them. My questions were where they are stored? how to track their ...
  • Consider Structs Consider Structs While taking birth in programming world, we all had our own experiences of learning; begin working on a new language or OOPS concepts or experimenting with a new tool or compilers. One of such expe...