BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Types of polymorphism in java- Runtime and Compile time polymorphism

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In the last tutorial we discussed Polymorphism in Java. In this guide we will see types of polymorphism. There are two types of polymorphism in java:
1) Static Polymorphism also known as compile time polymorphism
2) Dynamic Polymorphism also known as runtime polymorphism

Compile time Polymorphism (or Static polymorphism)

Polymorphism that is resolved during compiler time is known as static polymorphism. Method overloading is an example of compile time polymorphism.
Method Overloading: This allows us to have more than one method having the same name, if the parameters of methods are different in number, sequence and data types of parameters. We have already discussed Method overloading here: If you didn’t read that guide, refer: Method Overloading in Java

Example of static Polymorphism

Method overloading is one of the way java supports static polymorphism. Here we have two definitions of the same method add() which add method would be called is determined by the parameter list at the compile time. That is the reason this is also known as compile time polymorphism.

class SimpleCalculator
{
    int add(int a, int b)
    {
         return a+b;
    }
    int  add(int a, int b, int c)  
    {
         return a+b+c;
    }
}
public class Demo
{
   public static void main(String args[])
   {
	   SimpleCalculator obj = new SimpleCalculator();
       System.out.println(obj.add(10, 20));
       System.out.println(obj.add(10, 20, 30));
   }
}

Output:

30
60

Runtime Polymorphism (or Dynamic polymorphism)

It is also known as Dynamic Method Dispatch. Dynamic polymorphism is a process in which a call to an overridden method is resolved at runtime, thats why it is called runtime polymorphism. I have already discussed method overriding in detail in a separate tutorial, refer it: Method Overriding in Java.

Example
In this example we have two classes ABC and XYZ. ABC is a parent class and XYZ is a child class. The child class is overriding the method myMethod() of parent class. In this example we have child class object assigned to the parent class reference so in order to determine which method would be called, the type of the object would be determined at run-time. It is the type of object that determines which version of the method would be called (not the type of reference).

To understand the concept of overriding, you should have the basic knowledge of inheritance in Java.

class ABC{
   public void myMethod(){
	System.out.println("Overridden Method");
   }
}
public class XYZ extends ABC{

   public void myMethod(){
	System.out.println("Overriding Method");
   }
   public static void main(String args[]){
	ABC obj = new XYZ();
	obj.myMethod();
   }
}

Output:

Overriding Method

When an overridden method is called through a reference of parent class, then type of the object determines which method is to be executed. Thus, this determination is made at run time.
Since both the classes, child class and parent class have the same method animalSound. Which version of the method(child class or parent class) will be called is determined at runtime by JVM.

Few more overriding examples:

ABC obj = new ABC();
obj.myMethod();
// This would call the myMethod() of parent class ABC

XYZ obj = new XYZ();
obj.myMethod();
// This would call the myMethod() of child class XYZ

ABC obj = new XYZ();
obj.myMethod();
// This would call the myMethod() of child class XYZ

In the third case the method of child class is to be executed because which method is to be executed is determined by the type of object and since the object belongs to the child class, the child class version of myMethod() is called.

❮ PreviousNext ❯

Top Related Articles:

  1. How to write to file in Java using BufferedWriter
  2. Multithreading in java with examples
  3. Final Keyword In Java – Final variable, Method and Class
  4. Java – Static Class, Block, Methods and Variables
  5. Constructor Overloading in Java with examples

Tags: Java-OOPs

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Pinky says

    September 23, 2014 at 4:53 PM

    public static add(int a,int b)
    {
    }
    public int add(int a)
    {
    }
    this is overloading or not?

    Reply
    • rajajisubramanian says

      November 18, 2014 at 1:17 PM

      no its not a method overloading

      public static add(int a,int b)-its like a constructor description so its not a method and if its a constructor means the constructor wont be a static and its violate the inheritance rules in oops so the method name may or may not be have the return type, those method only be used in the method overloading,

      Reply
    • abhi says

      January 6, 2015 at 12:32 PM

      no it is not overloading … you will get compile time Error overloading can happen only either between static method or non static method..

      because non static is instance member and static is class member

      Reply
      • Shivam says

        May 30, 2015 at 10:21 PM

        There will be no compile time error
        It can be called as
        A.add(2,3); // A is name of the class
        a.add(2); // a is reference to object of class A

        Compile time error will only occur when number and type of arguments are same.

        Reply
    • Rajesh says

      December 23, 2015 at 3:37 PM

      Yes , it will overload. don’t think about static or non static. its just because of missing return type for the above static method and respected returning values.other wise rest of thing works fine.

      Reply
  2. INDnrj says

    November 12, 2014 at 12:22 PM

    Yes of course! static members take part in overloading but not in overriding

    Reply
  3. kiran kishore barik says

    December 6, 2014 at 5:10 PM

    no no that is not a overloading method . static method is a class level method , where as plain method is a object level method , so that both method is not same .

    Reply
    • Rahul says

      July 22, 2015 at 12:23 PM

      it will be not overload because overloading concept will be fulfil when both would be same like both method should be static or non-static.

      Reply
  4. santhosh says

    October 9, 2015 at 8:38 AM

    can any of you say what are built in functions for class and objects????

    Reply
    • Rohit says

      February 7, 2016 at 11:27 AM

      I dont think built in is the right phrase.
      But you have Object class functions which are available to any newly created Class whose object’s can take advantage of. Ex wait(), notify(), equals(), etc.

      Reply
  5. manish says

    October 23, 2015 at 4:50 PM

    May i know what is runtime polymorphism?
    because all i only can see here is to how to achieve runtime and compile time polymorphism.

    Reply
    • Shashi says

      December 3, 2015 at 12:37 PM

      The method overriding is an example of runtime polymorphism.

      Reply
    • Mona says

      March 11, 2017 at 4:20 PM

      A compile time polymorphism is handled during compilation, when the program is compiled, hence “compile-time”.

      A runtime polymorphism is handled “live” when the program is “run”, hence “run-time”

      Reply
  6. Neha says

    January 11, 2016 at 5:32 PM

    what is the difference between static and dynamic polymorphism?

    Reply
    • Saadat says

      May 1, 2016 at 9:39 PM

      Dynamic (run time) polymorphism is the polymorphism existed at run-time. Here, Java compiler does not understand which method is called at compilation time. Only JVM decides which method is called at run-time. Method overloading and method overriding using instance methods are the examples for dynamic polymorphism.

      Reply

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap