Polymorphism in Oops

Polymorphism in Oops

Last updated on 21st Sep 2020, Artciles, Blog

About author

Guna (Sr Project Manager - Java )

He is a TOP-Rated Domain Expert with 6+ Years Of Experience, Also He is a Respective Technical Recruiter for Past 3 Years & Share's this Informative Articles For Freshers

(5.0) | 17443 Ratings 511
  • Polymorphism is an important concept of object-oriented programming
  • Polymorphism means existing in many forms. 
  • Polymorphism is an object oriented programming feature that allows us to perform a single action in different ways.

Example of Polymorphism>

In real life, we, humans do it all the time. We become ‘Brother’, ‘Father’, ‘Son’, ‘Husband’… etc. And all these forms have different attributes associated.

  • Brother has an attribute of Understanding you.
  • Father has an attribute of Maturity
  • Son has an attribute of Responsibility
  • Husband has an attribute of Love (like love for spouse)
Subscribe For Free Demo

Error: Contact form not found.

But all these are ultimately forms of ‘You’. Or it can be said that they are derived from ‘You’. You become these (Father, Son..etc) depending upon what is required.

Types of Polymorphism in Java

Basically, there are two types of polymorphism in java. They are:

  • 1. Static polymorphism
  • 2. Dynamic polymorphism

Polymorphism can be either static or dynamic depending on how the method is called in a class.

types-of-polymorphism-in-java

Static Polymorphism 

  • A polymorphism that is exhibited during compilation is called static polymorphism in java. In the static polymorphism, the behavior of a method is decided at compile-time.Hence, Java compiler binds method calls with method definition/body during compilation.Therefore, this type of polymorphism is also called compile-time polymorphism. Since binding is performed at compile-time, it is also known as early binding. 
  • Compile-time polymorphism can be achieved/implemented by method overloading in java.
  • Method overloading is a mechanism in which a class has multiple methods having the same name but different signatures. It is one of the ways that Java implements polymorphism.
  • Another example of static polymorphism is constructor overloading and method hiding.

Compile-time Polymorphism Example Program

Let’s take an example program where we will implement static polymorphism. In this example program, we will create a class StaticPoly and in this class, we will create two methods having the same name sum. Both of these methods will have different signatures. So, let’s start coding.

Program source code 1:

  •   package staticPolymorphism;
  • public class StaticPoly
  • {
  •  void sum(int x, int y)
  •  {
  •    int s = x + y;
  •    System.out.println(“Sum of two numbers: ” +s);
  •  }
  •  void sum(int x, int y, int z)
  •  {
  •   int s = x + y + z;
  •   System.out.println(“Sum of three numbers: ” +s);
  •  }
  •  public static void main(String[] args)
  • {
  •  StaticPoly obj = new StaticPoly();
  •  obj.sum(20, 10);
  •         obj.sum(10, 20, 30);
  •  }
  • }

Output:

      Sum of two numbers: 30

      Sum of three numbers: 60

Explanation:

As you can observe in the preceding example program, sum() method is overloaded two times because the signatures of both methods are varying in the number of parameters.

The first sum() method accepts two parameters whereas, the second sum() method accepts three parameters. 

When we will call the first sum() method using reference variable “obj” by passing two int type argument values, Java compiler binds the definition of sum(int x, int y) method with sum(20, 10) method during compilation and calls the appropriate method. Hence, the sum of two numbers is displayed by invoking sum() method on the console.

Similarly, when we call the second sum() method by passing three int type argument values, Java compiler binds the definition of sum(int x, int y, int z) with sum(10, 20, 30) method during compilation, and calls a method of the sum of three numbers. 

Thus, Java compiler matches the values passed to a method during compilation, binds method call with appropriate method definition and calls the appropriate method.

In this way, compile-time polymorphism allows us to perform various operations by using multiple methods with the same name.

Dynamic Polymorphism in Java

  • A polymorphism that is exhibited at runtime is called dynamic polymorphism in java. In dynamic polymorphism, the behavior of a method is decided at runtime,therefore, the JVM (Java Virtual Machine) binds the method call with method definition/body at runtime and invokes the relevant method during runtime when the method is called.
  • This happens because objects are created at runtime and the method is called using an object of the class. The Java compiler has no awareness of the method to be called on an instance during compilation. Therefore, JVM invokes the relevant method during runtime.
  • Dynamic or runtime polymorphism can be achieved/implemented in java using method overriding.
  •  Method overriding is a mechanism where a method of Base class is overridden in the derived class to provide more specific implementation..

Runtime Polymorphism Example Program

Let’s take an example program where we will implement dynamic polymorphism by method overriding.

Let us consider two classes Base and Derived, as shown in the below code snippet. In both classes, we will declare a method named “calc” having the same signature.

Program source code 2:

  • package dynamicPoly;

    public class Base

    {

     void m1()

     {

       System.out.println(“m1-Base”);

     }

    }

    public class Derived extends Base

    {

     void m1()

     {

       System.out.println(“m1-Derived”);

     }

    }

    public class DynamicPoly

    {

     public static void main(String [] args)

     {

        Derived d = new Derived();

         d.m1(); // Calling m1() method of class Derived.

        Base b = new Base();

         b.m1(); // Calling m1() method of class Base.

      }

    }

   

Output:

      m1-Derived

      m1-Base

Explanation:

As you can observe in the above program, the class Base contains a method named m1. This m1() method is overridden in the derived class named Derived.

Since the m1() method in both Base and Derived classes have the same name and signature, therefore, the Java compiler is unable to bind method calls with method definitions.

Hence, on the basis of an object of a class, JVM decides to execute a suitable method at runtime. In this way, method overriding is one way to implement runtime polymorphism in java.

C And C Plus Plus Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

Advantages of using Polymorphism

  • Polymorphism is one of the core concepts of OOPS. This feature is applicable while handling various classes and subclasses. Here are some of the advantages of using polymorphism in programming.
  • One of the biggest advantages of using Polymorphism is that it allows the programming to extend itself.
  • This process allows the user to reuse the old classes and codes that were once tested and already implemented successfully. These codes and classes would be applicable to various other methods.
  • Through this process, the user would be able to store several variables of different types (double, Float, Int, Long, etc) in a single variable. This would make it easier for the user to search for and implement these variables.
  • Polymorphism would also help in reducing coupling between two different functions.

Are you looking training with Right Jobs?

Contact Us

Popular Courses