Polymorphism, one of the core concepts of OOP (Object-Oriented Programming) refers to the ability of an object to take different forms at different stages. For example, when connected to a computer, a USB cable acts as a data cable to transfer data, and the same cable, when connected to a charger adapter, acts as a power cabel. In Java, polymorphism results in code that are more concise.
There are 3 main types of Polymorphism in Java.
Another operand is converted to a higher type in an operation based on the operand type. For example, when you perform an operation between long and double, there is no operator to perform an operation of two different types. Hence, the compiler converts the long to a double (implicit conversion). Similarly, you can pass a subclass object for the method which has the superclass parameter.
Example
long l=200;
double d=300.34;
double sum=l+d;
In the above statement, ‘l’ is a long type and ‘d’ is a double type. But, when you perform addition, the compiler converts long to a double, adds ‘l’ to ‘d’ and returns a double value.
Overloading allows the same method or an operator to perform different actions based on the value. For example, when you use arithmetic operators such as +,-,/, etc., to perform an operation based on integer value/float value, it automatically performs the operation. Here, there is no need for a separate operator to perform any operation for the given types.
Also, we can have multiple methods with the same name, but the value type will be different for a method.
Consider the below program, which is used to search employee data based on employee Id, employee name, salary, or designation.
public Employee search(int empId)
{
//codeÂ
//return emp;
}
public Employee search(String employeeName)
public Employee search(double salary,String designation)
The above search methods take different types of parameters and return the value. Overloading is also called Static Binding.
A base class type, when serves as a subclass type, is called a subtype. When a subclass object is assigned to a base class type, and when a base class type is executed, it results in subclass type. For example, consider a bank account that has the account details of the customer. In programming, it must have an Account class with subclasses like SavingsAccount, FDAccount, LoanAccount, and many more. You have method getAccountDetails(), which will display common information. The subclasses produce specific data of the same method getAccountDetails().
import java.time.LocalDate;
import java.time.Month;
class Account
private int accountId;
private String customerName;
private double balance;
public Account(int accountId, String customerName, double balance)
{super();
this.accountId = accountId;
this.customerName = customerName;
this.balance = balance;
public void getAccountDetails()
System.out.println(“ID : “+this.accountId);
System.out.println(“Name : “+this.customerName);
System.out.println(“Balance : “+this.balance);
System.out.println(“—————————————-“);
class SavingsAccount extends Account
private double interestRate;
public SavingsAccount(int accountId, String customerName, double balance,double interestRate) {
super(accountId, customerName, balance);
this.interestRate=interestRate;
super.getAccountDetails();
System.out.println(“Interest : “+this.interestRate);
class FDAccount extends Account
private LocalDate maturityDate;
public FDAccount(int accountId, String customerName, double balance,double interestRate,LocalDate maturityDate) {
this.maturityDate=maturityDate;
System.out.println(“Maturity Date: “+this.maturityDate);
}}
public class TestAccount {
public static void displayAccountDetails(Account account)
account.getAccountDetails();
}public static void main(String[] args) {
// TODO Auto-generated method stub
displayAccountDetails(new Account(101,”Krithick”,5000));
displayAccountDetails(new SavingsAccount(201,”Chandrav”, 10000,4));
displayAccountDetails(new FDAccount(103,”Rajan”,25000, 6.25,
LocalDate.of(2021,Month.JUNE, 12)));
In the above example, the Account class has two subclasses – SavingsAccount and FDAccount, and the getAccountDetails() method needs to provide additional information, such as interest rate and maturity date. Hence the method is overridden in the subclasses.
The method displayAccountDetails() takes Account as a parameter and calls the getAccountDetails() method based on the type of object passed to the method.
Sample output
ID: 101
Name: Krithick
Balance: 5000.0
—————————————-
ID: 201
Name: Chandrav
Balance: 10000.0
Interest: 4.0
ID: 103
Name: Rajan
Balance: 25000.0
Interest: 6.25
Maturity: 2021-06-12
Compile Time Polymorphism
Runtime Polymorphism
CONCLUSION
Being one of the core concepts of OOP (Object-Oriented Programming), Polymorphism is an irreplaceable part of programming. Check out the UNext Jigsaw’s Postgraduate Certificate Program in Full Stack Development designed to aid learners in developing the mindset & competencies of a UI Developer & MEAN/MERN Stack Developer and transform them into well-rounded Full Stack Developer.
Fill in the details to know more
What Is the Use of Wrapper Class in Java?
March 22, 2023
What Is Clean Coding in Java?
March 21, 2023
What Are the New Features of Java 17?
What Is File Handling in Java?
March 16, 2023
What Is Data and Time Function in Java?
March 11, 2023
Top 10 Emerging Technologies Blogs To Read In 2023
December 15, 2022
History of Java Programming Language| UNext
November 18, 2022
An Advanced Guide to Shell Scripting in Unix!
November 15, 2022
Java Tutorial For Beginners
September 28, 2022
What Is Data Abstraction?
August 24, 2022
Encapsulation In Oops With Example
August 23, 2022