Easy way to IT Job

Polymorphism in OOPS
Share on your Social Media

Polymorphism in OOPS

Published On: October 21, 2023

Polymorphism means data can have many forms, combining “poly” (many) and “morph” (types). It’s an important concept in object-oriented programming (OOP).

Polymorphism is crucial in OOP, demonstrating that things may take on different shapes. It makes it easier to process different classes and objects using a single method in languages like Java or C++.

Polymorphism in OOP, in essence, allows you to redefine methods for derived classes. These polymorphic functions are frequently used in programming to implement inheritance; their functioning is dependent upon the data types being used.

In this blog, we will explore two types of Polymorphism in OOPs with illustrative examples.

Why is polymorphism used?

Polymorphism is used to accomplish a single task using several ways, allowing a single interface to support multiple implementations. The word “polymorphism” indicates that something can have multiple forms because “poly” means numerous and “morphs” means forms.

Types of Polymorphism in OOPs

There are two types of Polymorphism in OOPs. They are:

  • Static Binding (or Compile-time) Polymorphism, for example, Method Overloading.
  • Dynamic Binding (or Runtime) Polymorphism, for example, Method Overriding.

Static Polymorphism

Method Overloading in Object-Oriented Programming (OOPS) allows for static polymorphism, where multiple methods can be implemented with the same name but different parameters.

In OOPS, static polymorphism is achieved through method overloading. Java determines which method to call based on the method signature. Method overloading allows for the same method name but with a different number of arguments and return type. In simple terms, when an object is limited in its functionality, it’s called compile-time polymorphism.

Example of Compile time Polymorphism

Here is an example of compile time polymorphism, which is one of the types of Polymorphism in OOPs.

// Java program to demonstrate

// compile-time polymorphism

public class Calculation 

{

    // First multiplication function

    public static int multiply(int a, int b) 

    {

        return a * b;

    }

    // Second multiplication function

    public static double multiply(double a, double b) 

    {

        return a * b;

     }

    // Driver code

    public static void main(String args[]) {

        // Here, the first multiplication function is called

        System.out.println(multiply(5, 3));

        // Here, the second multiplication function is called

        System.out.println(multiply(4.0, 1.5));

    }

}

Output:

15

6.0

Static polymorphism conditions include:

Different types for all parameters.

  • Variations in the sequence of parameters
  • Varying the number of parameters between methods

Runtime Polymorphism

Dynamic Polymorphism, also known as Runtime Polymorphism, involves resolving a call to a single overridden method during a program’s runtime. A prominent example is Method Overriding, where this override is accomplished using pointers and virtual functions.

Runtime polymorphism is termed dynamic polymorphism because it determines the method to call during runtime, not at compile time. The compile-time compiler is unaware of this, as both methods share the same signature and can be called using any class object.

Example of Runtime Polymorphism in OOPs

// Java program to demonstrate runtime polymorphism

// Implementing a class

class Shape 

{

    // Implementing a method

    public void draw() 

    {

        System.out.println(“Drawing a shape”);

    }

}

// Defining a child class

public class Example extends Shape {

    // Overriding the parent method

    public void draw() 

{

        System.out.println(“Drawing a circle”);

    }

    // Driver code

    public static void main(String[] args) 

{

        Shape shape = new Example();

        shape.draw();

    }

}

Output:

Drawing a circle

Key points about Method Overriding and Runtime Polymorphism:

  • Method Overriding involves declaring a method in a sub-class within a parent class, allowing the child class to implement the method.
  • During Runtime Polymorphism, a class provides its own specifications to an inherited method, achieving this transition without altering the parent class’s object codes.

Advantages of Using Polymorphism

  • Code Reusability: Polymorphism makes it possible to reuse previously written and approved classes and code, which saves time and effort.
  • Simplified Data Handling: Data management is streamlined when several data types (such as Int, Float, Double, and Long) are stored in a single variable.
  • Debugging Made Easy: Polymorphism makes debugging easier.
  • Reduced Functionality Interconnection: It aids in maintaining and lowering the interdependencies among various functions.
  • Versatility: Depending on the circumstance, programmers can use one technique for multiple executions.
  • Code Extension: Polymorphism makes it easier to expand code and employ pre-existing programs.
  • Large-Scale Code Execution: Complex and large-scale code execution requires an understanding of this fundamental OOPs concept.
  • Improved Speed and Creativity: Programmers can write code more quickly and creatively because of polymorphism. 

Conclusion

Polymorphism is a key concept in Object-Oriented Programming (OOPS) languages, which are commonly used for a wide range of complex programming tasks. Programmes may be written more quickly and efficiently since it allows different object classes to be used through a single interface.

Consider enrolling in the best programming language course to enhance your knowledge and abilities in these popular and interesting field if you have a strong interest in learning more about the fascinating world of programming languages.

Share on your Social Media

Just a minute!

If you have any questions that you did not find answers for, our counsellors are here to answer them. You can get all your queries answered before deciding to join SLA and move your career forward.

We are excited to get started with you

Give us your information and we will arange for a free call (at your convenience) with one of our counsellors. You can get all your queries answered before deciding to join SLA and move your career forward.