20 Apr 2017

Difference Between Single and Multiple Inheritance


Inheritance smInheritance 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 by the derived class. There are many ways to achieve inheritance single, multiple, Hierarchical, Multilevel, Hybrid. Whereas our main topic of discussion is the difference between single and multiple 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 classes which are combinely inherited by only one derived class.

Content: Single Vs Multiple Inheritance

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion


Comparison Chart

BASIS FOR COMPARISONSINGLE INHERITANCEMULTIPLE INHERITANCE
BasicDerived class inherits a single base class.Derived class inherits two or more than two base class.
ImplementationClass derived_class : access_specifier base classClass derived _class: access_specifier base_class1, access_specifier base_class2, ....
Access 
Derived class access the features of single base classDerived class access the combined features of inherited base classes
VisibilityPublic, Private, ProtectedPublic, Private, Protected
Run timeRequire small amount of run time over headRequire additional runtime overhead as compared to single inheritance

Definition of Single Inheritance

In Single inheritance, there is a single base class and a single derived class. 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.
Let’s have a real life example we have two classes, an “account class” and a” saving account class” . Here “saving account class” can inherit the feature of “account class”, so “account class” 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 “account class” through public member functions of “account class” but can’t access them directly.
Implementation of Single Inheritance:
  1. # include<iostream>
  2. using name space std;
  3. class Account{
  4. int acc_no, ;
  5. public:
  6. float balance;
  7. void initialize(int x, int y){
  8. acc_no=x;
  9. balance=y;
  10. }
  11. int get_accno.(){
  12. return acc_no;
  13. }
  14. } ;
  15. class Saving_acc : public Account{
  16. float intrest_rate;
  17. public :
  18. //constructor of Saving_acc class
  19. Saving_acc(int c){
  20. interest_rate=c;
  21. }
  22. void display (){
  23. cout<<account no<< get_a(); // acc_no is private to Saving_acc
  24. cout<<"revised balance"<<balance*intrest_rate<<"\n";//balance is public to Saving_acc
  25. }
  26. };
  27. int main()
  28. {
  29. Saving_acc S(0.07); //constructor of Saving_acc initialized
  30. S.initialize(24494, 6000); // object of Saving_acc class called
  31. //the member function Account class
  32. S.display();
  33. 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.
Lets make it easier with the example we have three classes i.e. Bike, Car, and Vehicle . Now, “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, “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 private to Car but can be accessed by “Vehicle”.
Implementation of Multiple Inheritance:
  1. # include<iostream>
  2. using namespace std;
  3. class Bike{
  4. int engine_cost;
  5. ;
  6. public:
  7. void set_engine_cost(int x){
  8. engine_cost=x;
  9. }
  10. };
  11. class Car{
  12. protected:
  13. int Model_no;
  14. public:
  15. void set_Model_no(int p){
  16. Model_no=p;
  17. }
  18. };
  19. class Vehical: public Bike, public car{
  20. int no_of_wheels
  21. public:
  22. vehical(int w){
  23. no_of_wheels=w;
  24. cout<<"no of wheels"<<no_of_wheels;
  25. }
  26. void display_Vehical_info( ){
  27. cout<<"engine cost of bike"<< engine_cost<<"\n";// illegal because engine_cost is private
  28. //to Bikeand vehicle ca't access it directly.
  29. cout<<""Model no of car is"<< Model_no<<"\n // legal because Model_no is private
  30. //to Car but accessible by vehicle.
  31. }
  32. };
  33. int main(){
  34. Vehicle V(4);
  35. V.set_engine_cost(12000);
  36. V.set_Model_no(1021);
  37. V.display_Vehical_info( );
  38. }

Key Differences Between Single and Multiple Inheritance

  1. Single inheritance is one derived class having a single base class whereas, in multiple inheritance, has two or more than two base classes, but single derived class.
  2. 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 derived class, it becomes difficult to identify that which base class’s attribute or function it is it has to derive.
  3. Overhead issues are lesser in case of single inheritance. But in 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.
  4. Single inheritance is more towards specialization. Whereas multiple inheritance is more towards generalization.
  5. As single inheritance has less overhead it has less runtime as compared to multiple inheritance.

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 the access to members of a base class in a certain way, but it makes data more secure. Single inheritance is somehow more simpler and easy to implement than multiple inheritance. Inheritance reduces the size of object code, but also affects the run time of the program.

No comments:

Post a Comment

commnet here