20 Apr 2017

Difference Between Inheritance and Polymorphism


Inheritance and polymorphismInheritance allows, code reusability and the polymorphism is, the occurrence of one function with different form. The basic difference between inheritance and polymorphism is that inheritance allows the already existing code to be reused again in a program, and polymorphism provides a mechanism to dynamically decide what form of a function to be invoked.

Content: Inheritance Vs Polymorphism

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

Comparison Chart

BASIS FOR COMPARISONINHERITANCEPOLYMORPHISM
BasicInheritance is creating a new class using the properties of the already existing class.Polymorphism is basically a common interface for multiple form.
ImplementationInheritance is basically implemented on classes.Polymorphism is basically implemented on function/methods.
UseTo support the concept of reusability in OOP and reduces the length of code.Allows object to decide which form of the function to be invoked when, at compile time(overloading) as well as run time(overriding).
FormsInheritance may be a single inheritance, multiple inheritance, multilevel inheritance, hierarchical inheritance and hybrid inheritance.Polymorphism may be a compile time polymorphism (overloading) or run-time polymorphism (overriding).
ExampleThe class 'table' can inherit the feature of the class 'furniture', as a 'table' is a 'furniture'.The class 'study_table' can also have function 'set_color()' and a class 'Dining_table' can also have function 'set_color()' so, which form of the set_color() function to invoke can be decided at both, compile time and run time.

Definition of Inheritance:

Inheritance is one of the crucial features of OOP, which strongly support “reusability”. Reusability could be described as creating a new class by reusing the properties of the existing class. In inheritance, there is a base class, which is inherited by the derived class. When a class inherits any other class, the member(s) of the base class becomes the member(s) of a derived class.
The general form of inheriting a class is as follow:
  1. class derived-class-name : access-specifier base-class-name{
  2. // body of the class
  3. } ;
Here, the access specifier provides the mode of access (private, public, protected) to the member(s) in the base class to the derived class. If no access specifier is present, by default, it is considered as “private”. In C++, if the derived class is “struct” then the access specifier is “public” by default.
In C++, inheritance can be achieved in five forms. They can be classified as:-
  • Single Inheritance (only one super class )
  • Multiple Inheritance ( several superclasses )
  • Hierarchical Inheritance ( one super class, many subclasses)
  • Multiple Inheritance ( derived from a derived class)
In Java, the class inherits the other class by using the keyword “extends”. In Java, the base class is referred as a super class, and derived class is referred as a subclass. A subclass can not access those members of the base class, which are declared as “private”. The general form inheriting the class in Java is as follow.
  1. class derived-class-name extends base-class-name{
  2. // body of the class
  3. } ;
Java does not support the inheritance of multiple inheritance, whereas it supports multilevel hierarchy.  In Java, sometimes a super class may want to hide its implementation details, and it makes some part of that data “private”. As in Java, a subclass can not access the private members of the superclass and if a subclass wants to access or initialize those members, then Java provides a solution. The subclass can refer the members of its immediate superclass by using a keyword “super”. Remember, you can only access the members of the immediate superclass.
The ‘super’ has two general forms. The first is, it use to call the constructor of super class. The second is, to access the member of the superclass that has been hidden by the member of the subclass.
  1. //first form of calling the constructor.
  2. class supper_class{
  3. supper_class(argument_list){..} //constructor of super class
  4. };
  5. class sub_class extends supper_class {
  6. sub_class(argument_list){..} //constructor of sub_class
  7. super(argument_list); // sub_class calls the constructor of super class
  8. }
  9. };
  1. //second for of 'super'
  2. class supper_class{
  3. int i;
  4. }
  5. class sub_class extends supper_class {
  6. int i;
  7. sub_class(int a, int b){
  8. super.i=a; // 'i' of super class
  9. i=b; // 'i' of sub class
  10. }
  11. };

Definition of Polymorphism

The term polymorphism simply means ‘one function, multiple forms’. Polymorphism is achieved at both compile time and run time. The compile time polymorphism is achieved through “overloading” whereas, the run time polymorphism is achieved through “overriding”.
The polymorphism allows the object to decide “which form of the function to be invoked when” at both, compile time and run time.
Let’s discuss the first concept of overloading. In overloading, we define a function in class more than one time with different, data type and the number of parameters whereas the function to be overloaded must have the same return type. Most of the times the functions to the overloaded are constructors of the class.
  1. class overload{
  2. int a, b;
  3. public:
  4. int overload(int x){ // first overload() constructor
  5. a=x;
  6. return a;
  7. }
  8. int overload(int x, int y){ //second overload() constructor
  9. a=x;
  10. b=y;
  11. return a*b;
  12. }
  13. };
  14. int main(){
  15. overload O1;
  16. O1.overload(20); //first overload() constructor call
  17. O1.overload(20,40); // second overload() constructor call
  18. }
Now, let’s discuss the second form of polymorphism, i.e. overriding. The concept of overriding can only be implemented to the function of the classes which also implement the concept of inheritance. In C++, the function to be overridden is preceded by the keyword “virtual” in the base class and redefined in the derived class with the same prototype except the keyword “virtual”.
  1. class base{
  2. public:
  3. virtual void funct(){ //virtual function of base class
  4. cout<<"This is a base class's funct()";
  5. }
  6. };
  7. class derived1 : public base{
  8. public:
  9. void funct(){ //virtual function of base class redefined in derived1 class
  10. cout<<"This is a derived1 class's funct()";
  11. }
  12. };
  13. int main()
  14. {
  15. base *p, b;
  16. derived1 d1;
  17. *p=&b;
  18. p->funct(); //call to base class funct().
  19. *p=&d1;
  20. return 0;
  21. }

Key Differences Between Inheritance and Polymorphism

  1. Inheritance is creating a class that derives its feature from an already existing class. On the other hand, polymorphism is an interface that can be defined in multiple forms.
  2. Inheritance is implemented on the classes whereas, the polymorphism is implemented on methods/functions.
  3. As inheritance allows a derived class to use the elements and methods defined in the base class, the derived class does not need to define those elements or method it again so, we can say it increases code reusability and hence, reduces the length of the code. On the other hand, polymorphism makes it possible for an object to decide what form of the method it wants to invoke at both compile time and run time.
  4. The inheritance can be classified as single inheritance, multiple inheritance, multilevel inheritance, hierarchical inheritance and hybrid inheritance. On the other hand, polymorphism is classified as overloading and overriding.

Conclusion:

The Inheritance and polymorphism are interrelated concepts, as the dynamic polymorphism applies to the classes which also implement the concept of inheritance.

No comments:

Post a Comment

commnet here