DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Injecting Implementations With Jakarta CDI Using Polymorphism
  • Avoiding If-Else: Advanced Approaches and Alternatives
  • Dust: Open-Source Actors for Java

Trending

  • Medallion Architecture: Why You Need It and How To Implement It With ClickHouse
  • Top Book Picks for Site Reliability Engineers
  • Event-Driven Architectures: Designing Scalable and Resilient Cloud Solutions
  • A Developer's Guide to Mastering Agentic AI: From Theory to Practice
  1. DZone
  2. Data Engineering
  3. Data
  4. Polymorphism and Dynamic Binding in Java

Polymorphism and Dynamic Binding in Java

Learn about polymorphism in Java and the two types: compile-time and runtime. Plus, we take a look at demonstrations of how to achieve static and dynamic binding.

By 
Ankit Dixit user avatar
Ankit Dixit
·
Mar. 17, 22 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
9.4K Views

Join the DZone community and get the full member experience.

Join For Free

Polymorphism is an Object-Oriented-Programming concept. Whether you are new to Java programming or a person who has worked with Java for years, you should know what polymorphism is in Java and how it works. Most developers claim that they know the topic well but when it comes to other complex features like static and dynamic binding, they seem underconfident. A good understanding of polymorphism allows us to build efficient object-oriented programs.

What Is Polymorphism in Java?

Polymorphism is composed of two words:        

  • Poly: meaning “many”
  • Morphism: meaning “forms”

Thus polymorphism means having many forms. In the programming world, it is defined as the capability of a signal or message to be displayed in more than one form.

Real-Life Example

A person can exhibit many characteristics at the same time. For example, a mother at the same time can be a wife, daughter, sister, employee of a company, etc. Hence, a person can exhibit different characteristics under different conditions. This is known as polymorphism.

Diagram displaying all the identities a person can have

Importance of Polymorphism

Polymorphism is one of the most important features of any object-oriented programming language (for example, Java), With the help of polymorphism, a single task can be carried out in different ways. 

Types of Polymorphism

In Java, polymorphism is broadly classified into two categories:

  1. Compile-time polymorphism (static binding)    
  2. Runtime polymorphism (dynamic binding)

Compile-Time Polymorphism

Compile-time polymorphism is also known as static binding. This type of polymorphism can be achieved through function overloading or operator overloading. But in Java, it is restricted to function overloading as Java doesn’t provide the support to carry out operator overloading.

Function Overloading

When there are at least two functions or methods with the same function name but either the number of parameters they contain is different or at least one data type of the corresponding parameter is different (or both), then it is known as a function or method overloading and these functions are known as overloaded functions.

Example 1

Untill now, we have studied what function overloading is. Now let's try to demonstrate function overloading programmatically

Java
 
class Main {
 
    // Method 1
    // Method with 2 integer parameters
    static int Addition(int a, int b)
    {
 
        // Returns sum of integer numbers
        return a + b;
    }
    // Method 2
    // having the same name but with 2 double parameters
    static double Addition(double a, double b)
    {
        // Returns sum of double numbers
        return a + b;
    }
    public static void main(String args[]) {
        
        // Calling method by passing
        // input as in arguments
        System.out.println(Addition(12, 14));
        System.out.println(Addition(15.2, 16.1));
 
    }
}

You can run the above program from here.

Program explanation:

  • The above program consists of two static functions having the same name: Addition.
  • Here, both function contains the same number of parameters but their corresponding parameters are different.
  • Method 1 accepts two integer parameters whereas Method 2 accepts two parameters of double data type.
  • From the main function, we are calling the Addition(12, 14) function first. The parameters passed are integers (12 and 14), hence Method 1 would be called here.
  • Then we have called the Addition(15.2, 16.1) function. Since the parameters passed are double data type (15.2 and 16.1), Method 2 would be called this time.
  • This is how function overloading is achieved in Java on the basis of different data types of parameters.

Example 2

Consider the program given below:

Java
 
class Main {
 
    // Method 1
    // Method with 2 integer parameters
    static int Addition(int a, int b)
    {
 
        // Returns sum of integer numbers
        return a + b;
    }
 
    // Method 2
    // having the same name but with 3 integer parameters
    static double Addition(double a, double b)
    {
 
        // Returns sum of integer numbers
        return a + b;
    }
    public static void main(String args[]) {
        
        // Calling method by passing
        // input as in arguments
        System.out.println(Addition(12, 14));
        System.out.println(Addition(15.2, 16.1));
 
    }
}

You can run the above program from here. 

Program explanation:

  • The above program consists of two static functions with the same name: Addition.
  • Here, both functions contain a different number of parameters but the data type of the first two corresponding parameters is the same (integer).
  • Method 1 accepts two integer parameters and Method 2 accepts three parameters of integer data type.
  • From the main function, we are calling the Addition(2, 3) function first. Since the parameters passed are integers (2 and 3), Method 1 would be called here.
  • Then we have called the Addition(4, 5, 6) function. The parameters passed are double data types (4, 5, 6), so Method 2 would be called this time.
  • This is how function overloading is achieved in Java on the basis of a different number of parameters.

Example 3

Java
 
class Main {
 
    // Method 1
    // Method with 2 integer parameters
    static int Addition(int a, int b)
    {
        // Return the sum
        return a + b;
    }
    // Method 2
    // having the same name but with 3 parameters
    // 1st parameter is of type double and other parameters
    // are of type integer
    static double Addition(double a, int b,  int c)
    {
        // Return the sum
        return a + b + c;
    }
    public static void main(String args[]) {
        
        // Calling method by passing
        // input as in arguments
        System.out.println(Addition(2, 4));
        System.out.println(Addition(4.2, 6, 10));
 
    }
}

You can run the above program from here. 

Program explanation:

  • The above program consists of two static functions having the same name: Addition.
  • Both functions contain a different number of parameters and the data type of the first corresponding element is also different.
  • Method 1 accepts two integer parameters whereas Method 2 accepts three parameters — the first one is a double type and the other two are integer data types.
  • From the main function, we are calling the Addition(2, 4) function first. Since the parameters passed are integers (2 and 4),  Method 1 would be called here.
  • Then we have called the Addition(4.2, 6, 10) function. The first parameter passed is an integer type and other parameters are double data type (4.2, 6, 10), therefore, Method 2 would be called this time.
  • This is how function overloading is achieved in Java on the basis of a different number of parameters as well as the different data types of corresponding parameters.

Note: Function cannot be overloaded on the basis of the return type of functions only.

Runtime Polymorphism

This is also known as dynamic binding. In this process, a function call made to a function is resolved during runtime only. We can achieve dynamic binding in Java through method overriding. 

Method Overriding

Method overriding in Java occurs when a method in the base class has a definition in the derived class. The method or function in the base class is known as an overridden method.

Java
 
// Class 1
class Parent {
 
    // Print method
    void Print()
    {
 
        // Print statement
        System.out.println("Inside Parent Class");
    }
}
 
// Class 2
class Child1 extends Parent {
 
    // Print method
    void Print() { System.out.println("Inside Child1 Class"); }
}
 
// Class 3
class Child2 extends Parent {
 
    // Print method
    void Print()
    {
        // Print statement
        System.out.println("Inside Child2 Class");
    }
}
 
class Main {
 
    public static void main(String args[]) {
        
        // Creating an object of class Parent
        Parent parent = new Parent();
        parent.Print();
 
        // Calling print methods
        parent = new Child1();
        parent.Print();
 
        parent = new Child2();
        parent.Print();
    }
}

You can run the above program from here. 

Program explanation:

  • The above program consists of three classes: Parent (class 1), Child1 (class 2), and Child2 (class 3). class 2 and class 3 inherit class 1.
  • The Parent has a method called Print(). Inside this function, we are printing "Inside Parent Class". Child1 and Child2 also have Print() functions that basically override the Print() function of the class Parent and would print "Inside Child1 Class" and "Inside Child2 Class" respectively to the console.
  • From the main function, we are first creating an object of the parent class called a parent. Then, with the help of this object, we are calling the print method of the parent class. Therefore, "Inside Parent Class" would be printed on the console.
  • After that, we have called the default constructor of the Child1 class and we are calling the Print() function. Note that now the Print() method defined under the Child1 class would be called as we have overridden the Print() method of the parent class. Hence, "Inside Child1 Class" would be printed on the console.
  • Lastly, we have called the default constructor of the Child2 class and we are calling the Print() function. Here, the Print() method defined under the Child2 class would be called as we have overridden the Print() method of the parent class. Hence, “Inside Child2 Class” would be printed on the console.
  • This is how method overriding is achieved in Java.

Summary

In this article, we learned what polymorphism is in Java. Then we went deep into the topic and discussed two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism. We demonstrated through programs how we can achieve static and dynamic binding in Java.

Polymorphism (computer science) Java (programming language) Binding (linguistics) Data Types

Opinions expressed by DZone contributors are their own.

Related

  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Injecting Implementations With Jakarta CDI Using Polymorphism
  • Avoiding If-Else: Advanced Approaches and Alternatives
  • Dust: Open-Source Actors for Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!