Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

Polymorphism allows an object to take multiple forms – when a method exhibits polymorphism, the compiler has to map the name of the method to the final implementation.

If it’s mapped at compile time, it’s a static or early binding.

If it’s resolved at runtime, it’s known as dynamic or late binding.

2. Understanding Through a Code

When a subclass extends a superclass, it can re-implement methods defined in by it. This is called a method overriding.

For example, let’s create a superclass Animal:

public class Animal {

    static Logger logger = LoggerFactory.getLogger(Animal.class);

    public void makeNoise() {
        logger.info("generic animal noise");
    }

    public void makeNoise(Integer repetitions) {
        while(repetitions != 0) {
            logger.info("generic animal noise countdown " + repetitions);
            repetitions -= 1;
        }
    }
}

And a subclass Dog:

public class Dog extends Animal {

    static Logger logger = LoggerFactory.getLogger(Dog.class);
    
    @Override
    public void makeNoise() {
        logger.info("woof woof!");
    }

}

On overloading a method, like the makeNoise() of Animal class, the compiler will resolve the method and its code at compile time. This is an example of static binding.

However, if we assign an object of type Dog to a reference of type Animal, the compiler will resolve the function-code mapping at runtime. This is dynamic binding.

To understand how this work, let’s write a small code snippet to call the classes and its methods:

Animal animal = new Animal();

// calling methods of animal object
animal.makeNoise();
animal.makeNoise(3);

// assigning a dog object to reference of type Animal
Animal dogAnimal = new Dog();

dogAnimal.makeNoise();

The output of the above code will be:
com.baeldung.binding.Animal - generic animal noise 
com.baeldung.binding.Animal - generic animal noise countdown 3
com.baeldung.binding.Animal - generic animal noise countdown 2
com.baeldung.binding.Animal - generic animal noise countdown 1
com.baeldung.binding.Dog - woof woof!

Now, let’s create a class:

class AnimalActivity {

    public static void eat(Animal animal) {
        System.out.println("Animal is eating");
    }

    public static void eat(Dog dog) {
        System.out.println("Dog is eating");
    }
}

Let us add these the line to the main class:

AnimalActivity.eat(dogAnimal);

The output would be:

com.baeldung.binding.AnimalActivity - Animal is eating

This example shows that a static function undergoes static binding.

The reason is that subclasses cannot override static methods. If the subclass implemented the same method, it would hide the method of the superclass. Similarly, if a method is final or private, the JVM will do a static binding.

A static bound method isn’t associated with a particular object but rather is called on Type (class in Java). Execution of such a method is marginally faster.

Any other method is automatically a virtual method in Java by default. The JVM resolves such methods at runtime and this is dynamic binding.

The exact implementation depends on the JVM, but it would take a C++ like approach, where the JVM looks up the virtual table to decide on which object the method would be called.

3. Conclusion

Binding is an integral part of a language that implements polymorphism, it’s important to understand the implications of both static and dynamic binding to be sure that our applications are behaving as we want them to.

With that understanding, however, we are able to effectively use class inheritance as well as method overloading.

As always, the code is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.