Java Polymorphism | Definition | Types | Examples

What is the Java polymorphism definition?

Polymorphism Means a Many-form, It derived from 2 Greek words: The first word “poly” means many and another “morphs” means forms. So with using a Java polymorphism can do on an object in many forms.

Polymorphism possible when classes are related to each other by is-a Relationship (Inheritance).

Before starting first learn about Java Inheritance. In Inheritance, we inherit a property of another class, where property means methods, variables, etc.

The Advantage of Java polymorphism is allowed to use those Inherit methods and perform a different action in less effort without affecting the old codes of class and interface.

Java Polymorphism Definition Types Examples runtime static compile time

Polymorphism in Java is one of the OOPs pillar concepts. In this tutorial, we already see what is Polymorphism, so now we look at types of different examples.


Java polymorphism examples

Let’s come to practical practices, We have an Animal class with a sound() method. What subclasses can be for Animal class? Obviously as a real object like – Dog, cats, Rat, Duck, etc animals.

Java polymorphism example and diagram animal

As we know every animal has a different sound, so here Dog, Cat, Duck classes will have own sound() methods implementation. See the below example of Java polymorphism and how the single methods can change in many forms with uniform patterns.

Output: The animal makes a sound
Dog says: Bow Bow
Cat says: meow meow
Duck says: quack quack

Types of polymorphism in Java

Polymorphism in Java has two types, you will find a different name for it in many places.

Compile-time Polymorphism

Java method overloading is called Compile Time or Static or Compile time Polymorphism.

We have complete depth tutorial on it follow this link – Java Overloading tutorial

Look at an example and code for now.

Output: 18

Runtime Polymorphism

Java Methods overriding is called as RunTime or Dynamic Polymorphism.

Here is a complete tutorial based on it, follow the link – Java Override Tutorial

Let’s see the simple example and code.

Output: All Employee Salary Range is 10-50k
Manager Salary is 40k

Do comment if any doubt, suggestion, and example are required for this tutorial. Follow the upper link of the tutorial of better understanding and there are many rules to achieve Polymorphism.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Java Polymorphism Examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

One thought on “Java Polymorphism | Definition | Types | Examples

  1. public interface Mid13 {

    default int add(double p,int n) {
    return (int) (p*n);
    default void menu(int id,int t,double p,int n,double tt) {
    if(t == 0) {
    System.out.print(“Vat 7% : ” + p*7/100 + ” “);
    if(tt >= 50000) {
    System.out.print(“Vat 5% : ” + tt*5/100 + ” “);
    if(tt >= 100000) {
    System.out.print(“Vat10% : ” + tt*10/100 + ” “);

    System.out.format(“ID : %d Type : %d Price : %f Amount sole : %d Total : %f \n”,id,t,p,n,tt);




    public class Mid12 implements Mid13{
    int id;
    int type;
    double price;
    int number;
    double total;

    Mid12(int t,double p,int n) {
    id = id;
    type = t;
    price = p;
    number = n;
    total = Mid13.super.add(p,n);,type,price,number,total);


    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.List;
    import java.util.Random;

    public class Mid1 {

    public static void main(String[] args) {
    Random random = new Random();
    List menu = new ArrayList();
    int a = 1;


    menu.add(new Mid12(random.nextInt(100),random.nextInt(2),random.nextInt(5000),random.nextInt(50)));

    while(a <= 100);



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.