Inheritance is a method which can derive or construct new classes from the existing class. Here our main topic of discussion is the difference between single inheritance and multiple inheritance, two types of inheritance. In single inheritance, we do have only one base class, which is inherited by only one derived class. In multiple inheritance we have more than two base class which are combinely inherited by only one derived class.
Inheritance strongly supports the concept of reusability, i.e. newly created class reuses the properties of already existing ones. Access specifier decides the way in which the base class member will be inherited to the derived class. There are many ways to achieve inheritance single, multiple, Hierarchical, multilevel, hybrid.
Content: Single Vs Multiple Inheritance
Comparison Chart
Basis for Comparison | Single Inheritance | Multiple Inheritance |
---|---|---|
Basic | Derived class inherits a single base class. | Derived class inherits two or more than two base class. |
Implementation | Class derived_class : access_specifier base class | Class derived _class: access_specifier base_class1, access_specifier base_class2, .... |
Access | Derived class access the features of single base class | Derived class access the combined features of inherited base classes |
Visibility | Public, Private, Protected | Public, Private, Protected |
Run time | Require small amount of run time over head | Require additional runtime overhead as compared to single inheritance |
Constructs | Inheritance tree | Inheritance Directed Acyclic Graph (DAG) |
Definition of Single Inheritance
In Single inheritance, there is a single base class, and a single derived class. The derived class inherits the base class either publicly, protectedly and privately. The members of the base class can be accessed by derived class according to the access specifier specified during inheriting the base class.
Example
Let’s have a real-life example we have two classes, an “account class” and a” saving account class”. Saving account class can inherit the feature of account class, so account class will become base/ super/parent class for saving account class, and saving account class will be a derived class.
Here Account class’s attributes are acc_no (private) and balance (public),and member functions are initialize(), get_accno() which are public. Now Account class get inherited to Saving account class publicly so, all public member of account class can be directly accessed by Saving account class. It can access the private member of the Account class through public member functions of Account class but can’t access them directly.
Ambiguity in single inheritance
It occurs if the class members are defined with the same name in both the base and derived classes. As a result, it generates an error – i.e., “member is unambiguous”, therefore these names must exist without ambiguity. As in single inheritance, the derived class object will retrieve only the member function defined inside the derived class.
Implementation of single inheritance:
# include<iostream> using namespace std; class Account { int acc_no; public: float balance; void initialize(int x, int y) { acc_no=x; balance=y; } int get_accno() { return acc_no; } } ; class Saving_acc : public Account { float interest_rate; public : //constructor of Saving_acc Saving_acc(float c) { interest_rate=c; } void display () { cout<<"account number "<<get_accno(); // acc_no is private to Saving_acc cout<<"\n""revised balance "<<balance*interest_rate<<"\n";//balance is public to Saving_acc } }; int main() { Saving_acc S(0.07); //constructor of Saving_acc initialized S.initialize(24494, 6000); // object of Saving_acc class called //the member function of Account class S.display(); return 0; }
Definition of Multiple Inheritance
Multiple inheritance allows derived class to inherit combined features of more than one base class, i.e. we do have single derived class and multiple base classes. Every base class has to be inherited by mentioning the separate access specifier for each of them. A derived class can access the members of base classes based on the access specifier by which the base class is inherited.
Example
Let’s make it easier with the example we have three classes, i.e. Bike, Car, and Vehicle. Now, the ‘Vehicle’ can inherit the features of ‘Bike’ as well as ‘Car’. So ‘Vehicle’ becomes derived class and ‘Bike’, and ‘Car’ becomes the base class. Now here ‘Bike’ and ‘Car’ are publicly inherited by ‘Vehicle’ it can access all public member of ‘Bike’ and ‘Car’, but as we have an entity, Model_no protected in ‘Car’, so it is private to Car but can be accessed by Vehicle.
Ambiguity in multiple inheritance
In some circumstances where two base classes have member functions with the same name, while the class derived from both base classes has zero functions with this name. As the name of the member function is not only enough, the object of the derived class is not capable of accessing appropriate base function.
Implementation of multiple inheritance:
# include<iostream> using namespace std; class Bike{ public: int engine_cost; void set_engine_cost(int x){ engine_cost=x; } }; class Car{ protected: int Model_no; public: void set_Model_no(int p) { Model_no=p; } }; class Vehical: public Car, public Bike { int no_of_wheels; public: Vehical(int w) { no_of_wheels=w; cout<<"no of wheels - "<<no_of_wheels<<"\n"; } void display_Vehical_info( ){ cout<<"engine cost of bike - "<< engine_cost<<"\n"; // If the engine_cost could have declared as private, it would not be accessed by Bike and vehicle directly. cout<<"Model no of car is - "<< Model_no<<"\n"; // legal because Model_no is private to Car but accessible by vehicle. } }; int main() { Vehical V(4); V.set_engine_cost(12000); V.set_Model_no(1021); V.display_Vehical_info( ); }
Key Differences Between Single and Multiple Inheritance
- Single inheritance is one derived class having a single base class. On the contrary, multiple inheritance has two or more than two base classes, but single derived class.
- Multiple inheritance is quite confusing as here a single derived class inherit two or more base class. If the base classes have an attribute or function with the same name then for a derived class, it becomes difficult to identify which base class’s attribute or function it has to derive.
- Overhead issues are lesser in case of single inheritance. However, in the case of multiple inheritance the object construction or destruction invokes the constructors and destructor of the parent class in the class hierarchy, which increases the overhead.
- Single inheritance is more towards specialization. Conversely, multiple inheritance is more towards generalization.
- Due to less overhead in single inheritance it has less runtime than multiple inheritance.
Ambiguity Resolution
To overcome the ambiguity problem, we can use scope resolution operator (::) with the member function name to eliminate ambiguity between two base classes. We use scope resolution operator by defining a named instance within the derived class, which overrides ambiguous member functions.
It helps in referring any base member explicitly where a name can be accessed that has been redefined in the derived class.
Conclusion
Inheritance makes the programmer’s work easy as if one class is already formed, its features can be adapted by other if required. Although the access specifier limits access to members of a base class in a certain way, it makes data more secure. Single inheritance is somehow simpler and easy to implement than multiple inheritance. Inheritance reduces the size of object code but also affects the run time of the program.
Garimella Aahlad says
Good article. Thank You
Cheruiyot kiprotich says
So nice that can take me into success. nice job and may God guide you for more like this.
veeranjaneyulu says
very good concept mam thankyou…………!