How to differentiate between static and dynamic polymorphism in Java?

JavaJavaBeginner
Practice Now

Introduction

Polymorphism is a fundamental concept in Java programming that allows objects of different classes to be treated as objects of a common superclass. This tutorial will guide you through the differences between static and dynamic polymorphism in Java, covering method overloading and method overriding, and how they enable polymorphic behavior in your Java applications.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java/ProgrammingTechniquesGroup -.-> java/method_overriding("`Method Overriding`") java/ProgrammingTechniquesGroup -.-> java/method_overloading("`Method Overloading`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/inheritance("`Inheritance`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/polymorphism("`Polymorphism`") subgraph Lab Skills java/method_overriding -.-> lab-414009{{"`How to differentiate between static and dynamic polymorphism in Java?`"}} java/method_overloading -.-> lab-414009{{"`How to differentiate between static and dynamic polymorphism in Java?`"}} java/inheritance -.-> lab-414009{{"`How to differentiate between static and dynamic polymorphism in Java?`"}} java/oop -.-> lab-414009{{"`How to differentiate between static and dynamic polymorphism in Java?`"}} java/polymorphism -.-> lab-414009{{"`How to differentiate between static and dynamic polymorphism in Java?`"}} end

Understanding Polymorphism in Java

Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows objects of different classes to be treated as objects of a common superclass. In Java, polymorphism can be achieved through two main mechanisms: static polymorphism (method overloading) and dynamic polymorphism (method overriding).

Static Polymorphism: Method Overloading

Method overloading is a type of static polymorphism in Java, where a class can have multiple methods with the same name but with different parameters. The compiler determines which method to call based on the number, types, and order of the arguments passed at the time of the method call.

public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }

    public int add(int a, int b, int c) {
        return a + b + c;
    }
}

In the example above, the Calculator class has two add() methods, one with two parameters and one with three parameters. This is an example of method overloading, where the compiler can determine which add() method to call based on the arguments provided.

Dynamic Polymorphism: Method Overriding

Dynamic polymorphism, also known as method overriding, occurs when a subclass provides its own implementation of a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameter list as the method in the superclass.

public class Animal {
    public void makeSound() {
        System.out.println("The animal makes a sound");
    }
}

public class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("The dog barks");
    }
}

In the example above, the Dog class overrides the makeSound() method of the Animal class, providing its own implementation. When an object of the Dog class is used, the makeSound() method of the Dog class will be called, not the makeSound() method of the Animal class.

By understanding the concepts of static and dynamic polymorphism, Java developers can write more flexible and reusable code, allowing objects of different classes to be treated as objects of a common superclass.

Static Polymorphism: Method Overloading

Method overloading is a type of static polymorphism in Java, where a class can have multiple methods with the same name but with different parameters. The compiler determines which method to call based on the number, types, and order of the arguments passed at the time of the method call.

Defining Overloaded Methods

To define overloaded methods, you need to have the same method name but with different parameter lists. The parameter lists can differ in the number, types, or order of the parameters.

public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }

    public int add(int a, int b, int c) {
        return a + b + c;
    }

    public double add(double a, double b) {
        return a + b;
    }
}

In the example above, the Calculator class has three add() methods, each with a different parameter list.

Calling Overloaded Methods

When you call an overloaded method, the Java compiler will determine which method to invoke based on the arguments you provide.

Calculator calc = new Calculator();
int result1 = calc.add(2, 3);       // Calls the add(int, int) method
int result2 = calc.add(2, 3, 4);    // Calls the add(int, int, int) method
double result3 = calc.add(2.5, 3.7); // Calls the add(double, double) method

Benefits of Method Overloading

Method overloading provides several benefits:

  1. Improved Code Readability: Overloaded methods with meaningful names can make your code more intuitive and easier to understand.
  2. Flexibility: Overloaded methods allow you to provide multiple ways for users to interact with your code, making it more flexible and user-friendly.
  3. Reduced Code Duplication: Overloaded methods can help you avoid writing similar code multiple times, reducing duplication and making your code more maintainable.

By understanding the concept of method overloading, Java developers can write more expressive and versatile code, making their applications more robust and user-friendly.

Dynamic Polymorphism: Method Overriding

Dynamic polymorphism, also known as method overriding, occurs when a subclass provides its own implementation of a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameter list as the method in the superclass.

Defining Overridden Methods

To override a method, you need to create a method in the subclass with the same name, return type, and parameter list as the method in the superclass.

public class Animal {
    public void makeSound() {
        System.out.println("The animal makes a sound");
    }
}

public class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("The dog barks");
    }
}

In the example above, the Dog class overrides the makeSound() method of the Animal class, providing its own implementation.

Calling Overridden Methods

When you call a method on an object, the Java runtime determines which implementation of the method to use based on the actual type of the object, not the declared type of the reference variable.

Animal animal = new Dog();
animal.makeSound(); // Output: The dog barks

In the example above, even though the reference variable animal is declared as an Animal type, the makeSound() method of the Dog class is called because the actual type of the object is Dog.

Benefits of Method Overriding

Method overriding provides several benefits:

  1. Polymorphic Behavior: Overriding methods allows you to achieve polymorphic behavior, where objects of different classes can be treated as objects of a common superclass.
  2. Flexibility: Overriding methods allows you to customize the behavior of a class to suit specific needs, making your code more flexible and adaptable.
  3. Code Reuse: Overriding methods allows you to reuse the code in the superclass, while still providing your own implementation in the subclass.

By understanding the concept of method overriding, Java developers can write more flexible and extensible code, allowing their applications to adapt to changing requirements and user needs.

Summary

In this Java tutorial, you have learned about the two main types of polymorphism: static polymorphism (method overloading) and dynamic polymorphism (method overriding). You now understand how these polymorphic techniques allow Java programs to exhibit flexible and adaptable behavior, making your code more maintainable and scalable. By mastering the concepts of static and dynamic polymorphism, you can write more efficient and versatile Java applications that can handle a wide range of scenarios.

Other Java Tutorials you may like