Page 1 of 13
Home Whiteboard Online Compilers Practice Articles Tools
Java - Polymorphism
Polymorphism in Java
Polymorphism is the ability of an object to take on many forms. Polymorphism is an
important feature of Java OOPs concept and it allows us to perform multiple operations by
using the single name of any method (interface). Any Java object that can pass more than
one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic
since any object will pass the IS-A test for its own type and for the class Object.
Advertisement
Use of Polymorphism in Java
The most common use of polymorphism in OOP occurs when a parent class reference is
used to refer to a child class object.
It is important to know that the only possible way to access an object is through a
reference variable. A reference variable can be of only one type. Once declared, the type of
a reference variable cannot be changed.
[Link] w w .[Link]/java/java_polymorphism.htm 1/13
Page 2 of 13
The reference variable can be reassigned to other objects provided that it is not declared
final. The type of the reference variable would determine the methods that it can invoke on
the object.
A reference variable can refer to any object of its declared type or any subtype of its
declared type. A reference variable can be declared as a class or interface type.
Java Polymorphism Example
Let us look at an example.
public interface Vegetarian{}
public class Animal{}
public class Deer extends Animal implements Vegetarian{}
Now, the Deer class is considered to be polymorphic since this has multiple inheritance.
Following are true for the above examples −
A Deer IS-A Animal
A Deer IS-A Vegetarian
A Deer IS-A Deer
A Deer IS-A Object
When we apply the reference variable facts to a Deer object reference, the following
declarations are legal −
Deer d = new Deer();
Animal a = d;
Vegetarian v = d;
Object o = d;
All the reference variables d, a, v, o refer to the same Deer object in the heap.
Advertisement
Java Polymorphism Implementation
In this example, we're showcasing the above concept by creating the object of a Deer and
assigning the same to the references of superclasses or implemented interface.
[Link] w w .[Link]/java/java_polymorphism.htm 2/13
Page 3 of 13
interface Vegetarian{}
class Animal{}
public class Deer extends Animal implements Vegetarian{
public static void main(String[] args) {
Deer d = new Deer();
Animal a = d;
Vegetarian v = d;
Object o = d;
[Link](d instanceof Deer);
[Link](a instanceof Deer);
[Link](v instanceof Deer);
[Link](o instanceof Deer);
}
}
Output
true
true
true
true
Types of Java Polymorphism
There are two types of polymorphism in Java:
Compile Time Polymorphism
Run Time Polymorphism
Compile Time Polymorphism in Java
Advertisement
Compile-time polymorphism is also known as static polymorphism and it is implemented
by method overloading.
Example: Compile Time Polymorphism
This example has multiple methods having the same name to achieve the concept of
compile-time polymorphism in Java.
[Link] w w .[Link]/java/java_polymorphism.htm 3/13
Page 4 of 13
// Java Example: Compile Time Polymorphism
public class Main {
// method to add two integers
public int addition(int x, int y) {
return x + y;
}
// method to add three integers
public int addition(int x, int y, int z) {
return x + y + z;
}
// method to add two doubles
public double addition(double x, double y) {
return x + y;
}
// Main method
public static void main(String[] args) {
// Creating an object of the Main method
Main number = new Main();
// calling the overloaded methods
int res1 = [Link](444, 555);
[Link]("Addition of two integers: " + res1);
int res2 = [Link](333, 444, 555);
[Link]("Addition of three integers: " + res2);
double res3 = [Link](10.15, 20.22);
[Link]("Addition of two doubles: " + res3);
} Advertisement
}
Output
Addition of two integers: 999
Addition of three integers: 1332
Addition of two doubles: 30.369999999999997
[Link] w w .[Link]/java/java_polymorphism.htm 4/13
Page 5 of 13
Run Time Polymorphism in Java
Run time polymorphism is also known as dynamic method dispatch and it is implemented
by the method overriding.
Example: Run Time Polymorphism
// Java Example: Run Time Polymorphism
class Vehicle {
public void displayInfo() {
[Link]("Some vehicles are there.");
}
}
class Car extends Vehicle {
// Method overriding
@Override
public void displayInfo() {
[Link]("I have a Car.");
}
}
class Bike extends Vehicle {
// Method overriding
@Override
public void displayInfo() {
[Link]("I have a Bike.");
}
}
public class Main {
Advertisement
public static void main(String[] args) {
Vehicle v1 = new Car(); // Upcasting
Vehicle v2 = new Bike(); // Upcasting
// Calling the overridden displayInfo() method of Car class
[Link]();
// Calling the overridden displayInfo() method of Bike class
[Link] w w .[Link]/java/java_polymorphism.htm 5/13
Page 6 of 13
[Link]();
}
}
Output
I have a Car.
I have a Bike.
Virtual Method and Run Time Polymorphism in Java
In this section, I will show you how the behavior of overridden methods in Java allows you
to take advantage of polymorphism when designing your classes.
We already have discussed method overriding, where a child class can override a method
in its parent. An overridden method is essentially hidden in the parent class, and is not
invoked unless the child class uses the super keyword within the overriding method.
Example: Implementation of Run Time Polymorphism with Virtual
Methods
/* File name : [Link] */
public class Employee {
private String name;
private String address;
private int number;
public Employee(String name, String address, int number) {
[Link]("Constructing an Employee");
[Link] = name;
[Link] = address;
[Link] = number;
} Advertisement
public void mailCheck() {
[Link]("Mailing a check to " + [Link] + " " +
[Link]);
}
public String toString() {
return name + " " + address + " " + number;
[Link] w w .[Link]/java/java_polymorphism.htm 6/13
Page 7 of 13
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public void setAddress(String newAddress) {
address = newAddress;
}
public int getNumber() {
return number;
}
}
Now suppose we extend Employee class as follows −
/* File name : [Link] */
public class Salary extends Employee {
private double salary; // Annual salary
public Salary(String name, String address, int number, double salary) {
super(name, address, number);
setSalary(salary);
}
public void mailCheck() {
[Link]("Within mailCheck of Salary class ");
[Link]("Mailing check to " + getName()
Advertisement
+ " with salary " + salary);
}
public double getSalary() {
return salary;
}
public void setSalary(double newSalary) {
if(newSalary >= 0.0) {
[Link] w w .[Link]/java/java_polymorphism.htm 7/13
Page 8 of 13
salary = newSalary;
}
}
public double computePay() {
[Link]("Computing salary pay for " + getName());
return salary/52;
}
}
Now, you study the following program carefully and try to determine its output −
/* File name : [Link] */
public class VirtualDemo {
public static void main(String [] args) {
Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00);
Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);
[Link]("Call mailCheck using Salary reference --");
[Link]();
[Link]("\n Call mailCheck using Employee reference--");
[Link]();
}
}
class Employee {
private String name;
private String address;
private int number;
public Employee(String name, String address, int number) {
Advertisement
[Link]("Constructing an Employee");
Chapters Categories
[Link] = name;
[Link] = address;
[Link] = number;
}
public void mailCheck() {
[Link]("Mailing a check to " + [Link] + " " +
[Link] w w .[Link]/java/java_polymorphism.htm 8/13
Page 9 of 13
[Link]);
}
public String toString() {
return name + " " + address + " " + number;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public void setAddress(String newAddress) {
address = newAddress;
}
public int getNumber() {
return number;
}
}
class Salary extends Employee {
private double salary; // Annual salary
public Salary(String name, String address, int number, double salary) {
super(name, address, number);
setSalary(salary);
}
public void mailCheck() {
[Link]("Within mailCheck of Salary class ");
Advertisement
[Link]("Mailing check to " + getName()
+ " with salary " + salary);
}
public double getSalary() {
return salary;
}
[Link] w w .[Link]/java/java_polymorphism.htm 9/13
Page 10 of 13
public void setSalary(double newSalary) {
if(newSalary >= 0.0) {
salary = newSalary;
}
}
public double computePay() {
[Link]("Computing salary pay for " + getName());
return salary/52;
}
}
Output
Constructing an Employee
Constructing an Employee
Call mailCheck using Salary reference --
Within mailCheck of Salary class
Mailing check to Mohd Mohtashim with salary 3600.0
Call mailCheck using Employee reference--
Within mailCheck of Salary class
Mailing check to John Adams with salary 2400.0
Here, we instantiate two Salary objects. One using a Salary reference s, and the other
using an Employee reference e.
While invoking [Link](), the compiler sees mailCheck() in the Salary class at compile
time, and the JVM invokes mailCheck() in the Salary class at run time.
mailCheck() on e is quite different because e is an Employee reference. When the compiler
sees [Link](), the compiler sees the mailCheck() method in the Employee class.
Here, at compileAdvertisement
time, the compiler used mailCheck() in Employee to validate this
statement. At run time, however, the JVM invokes mailCheck() in the Salary class.
This behavior is referred to as virtual method invocation, and these methods are referred
to as virtual methods. An overridden method is invoked at run time, no matter what data
type the reference is that was used in the source code at compile time.
[Link] w w .[Link]/java/java_polymorphism.htm 10/13
Page 11 of 13
TOP TUTORIALS
Python Tutorial
Java Tutorial
C++ Tutorial
C Programming Tutorial
C# Tutorial
PHP Tutorial
R Tutorial
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
SQL Tutorial
TRENDING TECHNOLOGIES
Cloud Computing Tutorial
Amazon Web Services Tutorial
Microsoft Azure Tutorial
Git Tutorial
Ethical Hacking Tutorial
Docker Tutorial
Kubernetes Tutorial
DSA Tutorial
Spring Boot Tutorial
SDLC Tutorial
Unix Tutorial
CERTIFICATIONS
Advertisement
Business Analytics Certification
Java & Spring Boot Advanced Certification
Data Science Advanced Certification
Cloud Computing And DevOps
Advanced Certification In Business Analytics
Artificial Intelligence And Machine Learning
DevOps Certification
[Link] w w .[Link]/java/java_polymorphism.htm 11/13
Page 12 of 13
Game Development Certification
Front-End Developer Certification
AWS Certification Training
Python Programming Certification
COMPILERS & EDITORS
Online Java Compiler
Online Python Compiler
Online Go Compiler
Online C Compiler
Online C++ Compiler
Online C# Compiler
Online PHP Compiler
Online MATLAB Compiler
Online Bash Terminal
Online SQL Compiler
Online Html Editor
ABOUT US | OUR TEAM | CAREERS | JOBS | CONTACT US | TERMS OF USE |
PRIVACY POLICY | REFUND POLICY | COOKIES POLICY | FAQ'S
Advertisement
Tutorials Point is a leading Ed Tech company striving to provide the best learning material on
technical and non-technical subjects.
[Link] w w .[Link]/java/java_polymorphism.htm 12/13
Page 13 of 13
© Copyright 2025. All Rights Reserved.
Advertisement
[Link] w w .[Link]/java/java_polymorphism.htm 13/13