Polymorphism in java with example

In this tutorial, we will see about Polymorphism in java.

Polymorphism in java is one of core Object oriented programming concepts with Abstraction, encapsulation, and inheritance.

Polymorphism means one name many forms. In Java, polymorphism can be achieved by method overloading and method overriding.

There are two types of polymorphism in java.

  • Compile time polymorphism.
  • Run time polymorphism.

Compile time Polymorphism

Compile time Polymorphism is nothing but method overloading in java. You can define various methods with same name but different method arguments. You can read more about method overloading.

Let’s understand with the help of example:

When you run above program, you will get below output:

Integer: 20
Double 30.0
Integer a and b:20 30

As you can see here, we have used same method name but different method arguments.The compiler will call appropriate method based on best-matched arguments.

Runtime Polymorphism

Runtime Polymorphism is nothing but method overriding in java.If subclass is having same method as base class then it is known as method overriding Or in another word, If subclass provides specific implementation to any method which is present in its one of parents classes then it is known as method overriding.

Let’s say you have parent class as Shape and child class as Rectangle and circle.

When you run above program, you will get below output:

Drawing Rectangle
Drawing Circle

Please note that we are assigning child object to parent object here.

As you can see we have overridden draw methods in child class Rectangle and Circle.JVM decides at runtime which method it needs to call depending on the object assignment. That’s why this is known as Run time polymorphism.

That’s all about Polymorphism in java.

Was this post helpful?

Related Posts

  • 30 January

    Difference between early binding and late binding in java

    Table of ContentsEarly Binding or Static BindingWhy are static final and private methods associated by Early Binding?Code Example:Analysis:Late Binding or Dynamic BindingCode Example:Output:Analysis:Differences between Static and Dynamic Binding Binding in Java refers to the process of associating a method or function body with a method or function call by the Java Compiler. In simpler words, […]

  • 04 November

    Can we overload main method in java

    In this post, we will see about "Can we overload main method in java".This is one of the most asked Core java interview questions. Yes, we can overload main method in java but when you run your program, JVM will search for public static void main(String[] args) and execute that method. Overload main method in […]

  • 02 November

    Dynamic method dispatch in java

    In this post, we will about Dynamic method dispatch which is also referred as run time polymorphism. Dynamic Method Dispatch Dynamic method dispatch is a technique by which call to a overridden method is resolved at runtime, rather than compile time.When an overridden method is called by a reference, then which version of overridden method […]

  • 29 October

    Can we override static method in java

    No, we can not override static method in java. Static methods are those which can be called without creating object of class,they are class level methods. On other hand,If subclass is having same method signature as base class then it is known as method overriding. Its execution decided at run time. Below are the reasons why […]

  • 26 August

    Copy Constructor in java

    In this post, we will see about copy constructor in java. Copy constructor is the constructor which takes parameter as object of same class and copy each field of the class to the new object. Unlike C++, java does not provide default copy contructor. You need to create one, if you want to have copy […]

  • 26 August

    Java default constructor

    In this post, we will see about Java default constructor. Default constructor is the no arg constructor which is inserted by compiler unless you provide any other constructor explicitly. You won’t able to see it as it is present in class file rather than source file. Is there any difference between no argument constructor and […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.