Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring 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);
    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

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


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:


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.

Java bottom

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

Generic footer banner
Comments are closed on this article!