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 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 of those Inherit methods and performs a different action in less effort without affecting the old codes of class and interface.
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, rats, Duck, etc animals.
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.
class Animal { public void sound() { System.out.println("The animal makes a sound"); } } class Dog extends Animal { public void sound() { System.out.println("Dog says: Bow Bow"); } } class Cat extends Animal { public void sound() { System.out.println("Cat says: meow meow"); } } class Duck extends Animal { public void sound() { System.out.println("Duck says: quack quack"); } } public class PolymorphismExample { public static void main(String arg[]) { //Creating objects Animal animal = new Animal(); //Animal object Animal dog = new Dog(); //Dog object Animal cat = new Cat(); //Cat object Animal duck = new Duck(); // Duck object animal.sound(); dog.sound(); cat.sound(); duck.sound(); } }
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 (static binding) – Java Method overloading is an example of static polymorphism
- Runtime polymorphism (dynamic binding) – Java Method Overriding is an example of dynamic polymorphism.
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.
public class MethodOverloading { static int add(int a, int b) { return a + b; } static int add(int a, int b, int c) { return a + b + c; } public static void main(String[] args) { System.out.println(MethodOverloading.add(2, 16)); System.out.println(MethodOverloading.add(78, 1, 9)); } }
Output: 18
88
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.
//base class class Employee { //method void salary() { System.out.println("All Employee Salary Range is 10-50k"); } } //subclass class Manager extends Employee{ @Override void salary(){ System.out.println("Manager Salary is 40k"); } } public class MethodOverriding { public static void main(String arg[]){ // creating base (parent) class object Employee employee = new Employee(); employee.salary(); // creating sub (child) class object Employee employee1 = new Manager(); employee1.salary(); } }
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.1Java version 11
All Java Polymorphism Examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.
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);
Mid13.super.menu(this.id,type,price,number,total);
}
}
——–
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
public class Mid1 {
public static void main(String[] args) {
Random random = new Random();
List menu = new ArrayList();
int a = 1;
do
{
menu.add(new Mid12(random.nextInt(100),random.nextInt(2),random.nextInt(5000),random.nextInt(50)));
a++;
}
while(a <= 100);
}
}