devxlogo

Explore Polymorphism in Java

Explore Polymorphism in Java

Polymorphism is the existence of the same thing in various forms. It is a feature of OOP that enables items to change form based on circumstances. The term polymorphism is derived from two Greek words, namely, “poly” and “morphos.” While the term “poly” means many, and “morphos” which implies forms. Hence, polymorphism denotes the existence of the same thing in various forms. The Java Programming Language has been one of the most popular programming languages for decades. It is widely used in enterprise software development and has matured a lot over the last decade. The Java Programming Language provides excellent support for polymorphism.

In this article, we will explore how polymorphism can be implemented in Java. This article presents a discussion on what polymorphism is, types of polymorphism and how it can be implemented in Java with code examples wherever necessary to illustrate the concepts covered.

Types of Polymorphism

Polymorphism enables an object to exhibit different behavior based on circumstances. Essentially, polymorphism is of two types. These include: static or compile time polymorphism and dynamic or runtime polymorphism. You would need to take advantage of method overloading to implement static polymorphism and method overriding to implement runtime polymorphism.

Binding is the act of associating an instance of a class with its member. There are two types of binding: early binding and late binding. The basic difference between late binding and early binding is that in the former, the binding happens late, i.e., at runtime. Hence the name late binding.

Implementing Method Overloading

Method overloading is an example of early binding or compile-time polymorphism or static polymorphism. In this section, we will examine how we can implement method overloading in Java. Method overloading is a form of polymorphism in which a class can have multiple methods with identical names but different signatures. Consider the following class.

public class Test {     public int Add(int x, int y)    {        return x + y;    }    public double Add(double x, double y)    {        return x+y;    }     public String Add(String s1, String s2)     {       return s1+s2;     }}

Now refer to the code listing provided above. The class Test contains three methods — all of them having identical names. They differ in their signatures though. Hence, we can say that the Add method here is overloaded. Now, when you invoke an Add method and pass the necessary parameters, the correct version of the overloaded Add method would be called based on the type and sequence of the parameters passed.

Operator overloading is another example of method overloading. In operator overloading you overload operators to make it possible for user defined data types to work like fundamental data types. Though Java doesn’t have support for operator overloading by default, languages like C++ and C# have excellent support for operator overloading. You should note that a few operators in the String class in Java are overloaded.

Implementing Method Overriding

Method overriding is an example of dynamic binding or late binding or runtime polymorphism. Let’s understand method overriding with an example. Consider the following class called Account. The Account class contains one method called Deposit.

public class Account {    public void Deposit()    {        System.out.println("Deposit method of the Account class.");    }}

The SavingsAccount class extends the Account class as shown below. The SavingsAccount class extends the Account class and provides its own implementation of the Deposit method.

public class SavingsAccount extends Account {    public void Deposit()    {        System.out.println("Deposit method of the SavingsAccount class.");    }}

The following code snippet shows how you can create an instance of the SavingsAccount class and then invoke the Deposit method.

public static void main(String[] args) {        Account obj = new SavingsAccount();        obj.Deposit();    }

In the code example shown above, the SavingsAccount class inherits the members of the Account class and the Deposit method is overridden. The following statement is an example of upcasting.

Account obj = new SavingsAccount();

This is an example of runtime polymorphism. Note that upcasting is a type of polymorphism in which you can cast a type up in the inheritance chain. In other words, in upcasting, you can cast a subtype to a supertype. When the code above is executed, the message “Deposit method of the SavingsAccount class.” is displayed at the console window.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist