Difference between Inheritance and Polymorphism

05 Jun, 2022
Inheritance: Inheritance is one in which a new class is created that inherits the properties of the already exist class. It supports the concept of code reusability and reduces the length of the code in object-oriented programming.Types of Inheritance are:  
Single inheritanceMulti-level inheritanceMultiple inheritanceHybrid inheritanceHierarchical inheritance
Single inheritance
Multi-level inheritance
Multiple inheritance
Hybrid inheritance
Hierarchical inheritance
Example of Inheritance:  
C++
Java
#include "iostream"using namespace std; class A {    int a, b; public:    void add(int x, int y)    {        a = x;        b = y;        cout << (a + b) << endl;    }}; class B : public A {public:    void print(int x, int y)    {        add(x, y);    }}; int main(){    B b1;    b1.print(5, 6);}
class A {    int a, b;    public void add(int x, int y)    {        a = x;        b = y;        System.out.println("addition of a + b is:" + (a + b));    }}class B extends A {    public void sum(int x, int y)    {        add(x, y);    }     // Driver Code    public static void main(String[] args)    {        B b1 = new B();        b1.sum(5, 6);    }}
Output:  
addition of a+b is:11 
Here, class B is the derived class which inherit the property(add method) of the base class A.Polymorphism: Polymorphism is that in which we can perform a task in multiple forms or ways. It is applied to the functions or methods. Polymorphism allows the object to decide which form of the function to implement at compile-time as well as run-time.Types of Polymorphism are:  
Compile-time polymorphism (Method overloading)Run-time polymorphism (Method Overriding)
Compile-time polymorphism (Method overloading)
Run-time polymorphism (Method Overriding)
Example of Polymorphism:  
C++
Java
#include "iostream"using namespace std; class A {    int a, b, c; public:    void add(int x, int y)    {        a = x;        b = y;        cout << "addition of a+b is:" << (a + b) << endl;    }     void add(int x, int y, int z)    {        a = x;        b = y;        c = z;        cout << "addition of a+b+c is:" << (a + b + c) << endl;    }     virtual void print()    {        cout << "Class A's method is running" << endl;    }}; class B : public A {public:    void print()    {        cout << "Class B's method is running" << endl;    }}; int main(){    A a1;     // method overloading (Compile-time polymorphism)    a1.add(6, 5);     // method overloading (Compile-time polymorphism)    a1.add(1, 2, 3);     B b1;     // Method overriding (Run-time polymorphism)    b1.print();}
class A {    int a, b, c;     public void add(int x, int y)    {        a = x;        b = y;        System.out.println("addition of a+b is:" + (a + b));    }     public void add(int x, int y, int z)    {        a = x;        b = y;        c = z;        System.out.println("addition of a+b+c is:" + (a + b + c));    }     public void print()    {        System.out.println("Class A's method is running");    }}; class B extends A {    public void print()    {        System.out.println("Class B's method is running");    }     // Driver Code    public static void main(String[] args)    {        A a1 = new A();         // method overloading (Compile-time polymorphism)        a1.add(6, 5);         // method overloading (Compile-time polymorphism)        a1.add(1, 2, 3);         B b1 = new B();         // Method overriding (Run-time polymorphism)        b1.print();    }}
Output:  
addition of a+b is:11
addition of a+b+c is:6
Class B's method is running 
Difference between Inheritance and Polymorphism: 
Example :
The class bike can be inherit from the class of two-wheel vehicles, which is turn could be a subclass of vehicles.  
Example : 
The class bike can have method name set_color(), which changes the bike’s color based on the name of color you have entered. 
 
sapnasingh4991
stsaturian
nyctophiliac1918
priyansh70890
cpp-class
C++
Difference Between
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
Friend class and function in C++
Pair in C++ Standard Template Library (STL)
std::string class in C++
Class method vs Static method in Python
Difference between BFS and DFS
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Differences between JDK, JRE and JVM