EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Java

Polymorphism

Inheritance:

Inheritance is a feature in object-oriented programming where a new class (called derived or child class) is created by inheriting properties and behaviors (methods and variables) from an existing class (called base or parent class). It promotes code reusability and reduces redundancy.

Types of Inheritance:

1. Single Inheritance
2. Multi-level Inheritance
3. Multiple Inheritance
4. Hybrid Inheritance
5. Hierarchical Inheritance

Example of Inheritance:

#include <iostream>
using namespace std;

class A {
    int a, b;

public:
    void add(int x, int y)
    {
        a = x;
        b = y;
        cout << "Addition of a + b is: " << (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);
    return 0;
}

Output:

Addition of a + b is: 11

Here, class B inherits the add() method from class A.

Polymorphism:

Polymorphism is a feature in object-oriented programming where an object can take multiple forms. Polymorphism allows one task to be performed in different ways, either at compile-time or run-time.

Types of Polymorphism:

  1. Compile-time polymorphism (Method Overloading)
  2. Run-time polymorphism (Method Overriding)

Example of Polymorphism:

#include <iostream>
using namespace std;

class A {
    int a, b, c;

public:
    // Compile-time polymorphism (Method Overloading)
    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;
    }

    // Run-time polymorphism (Method Overriding)
    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;

    // Compile-time polymorphism (Method Overloading)
    a1.add(6, 5);
    a1.add(1, 2, 3);

    B b1;

    // Run-time polymorphism (Method Overriding)
    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:

Polymorphism
 InheritancePolymorphism
 Inheritance allows a new class (derived class) to inherit features from an existing class (base class).Polymorphism allows methods to take multiple forms.
 Inheritance applies to classes.Polymorphism applies to methods or functions.
 Inheritance supports code reusability and reduces code duplication.Polymorphism allows the program to choose which function to execute at compile-time (overloading) or run-time (overriding).
 Inheritance can be single, multiple, hierarchical, multilevel, or hybrid.Polymorphism can be compile-time (overloading) or run-time (overriding).
 Inheritance is used to model relationships between classes.Polymorphism allows flexibility in implementing methods.

Example of Inheritance:
A class Car can be derived from a class Vehicle, and Car can further inherit properties like engine type, wheels, etc.

Example of Polymorphism:
The class Car can have a method setColor(), which changes the car’s color based on the input color value provided.

End of lesson.