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.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as