Polymorphism in Java with Examples

In this article, we will learn polymorphism in Java with examples. I will try to explain this concept with the Java interface way. Are you ready? Let’s get started!

In simple English, polymorphism in Java is each class can do the same operation in a different way. I will explain this concept with cars. Let’s assume that, we have three car brands and each of them has to start their engine in a different way. Here, I want to emphasize that each car does the same operation which is starting the engine but they do this operation in a different way. How do they start the engine then? Let’s create our requirements and then implement them with our lovely language Java. 

We have Mercedes, Tesla, and Porsche. Hmmm, it seems we are really rich guys! :D They both start the engine in this way:

Mercedes: I am Mercedes! Starting the AMG Engine now!

Tesla: I am Porsche! Starting the Turbo Engine now!

Porsche: I am Tesla! Starting the Electric Engine now!

As I said they both do the same operation which is startEngine but as you see they both do this in a different way and this is called polymorphism in programming.

Now, let’s implement this behavior by using the Java Interface way.

Interfaces have a HAS-A relationship in programming. So, we have cars and each car has an engine. So, Engine is an interface for our cars and each of our cars implements this interface’s methods in a different way by using @override annotation. Let’s code it!

Polymorphism in Java Example with Interfaces

Engine Class is our interface and it has a startEngine method. We have also the default method which is getVehicleType and we do not need to implement default methods of interfaces if it is not necessary. I wanted to add a default method to show you how we are using them in interfaces. ;) 

public interface Engine {
    String vehicleType = "I am a car!";

    default String getVehicleType() {
        return vehicleType;
    }

    void startEngine();
}

Now, our cars will implement startEngine() method in a different way by @override annotation.

public class Mercedes implements Engine {
    @Override
    public void startEngine() {
        System.out.println("I am Mercedes! Starting the AMG Engine now!");
    }
}
public class Porsche implements Engine {
    @Override public void startEngine() {
        System.out.println("I am Porsche! Starting the Turbo Engine now!");
    }
}
public class Tesla implements Engine {
    @Override public void startEngine() {
        System.out.println("I am Tesla! Starting the Electric Engine now!");
    }
}

That’s it! This is polymorphism in JAVA :-) Do you want to test it? Let’s write a JUnit test to test how our cars can start their engines?

public class EngineTest {
    Tesla    tesla    = new Tesla();
    Mercedes mercedes = new Mercedes();
    Porsche  porsche  = new Porsche();

    @Test
    public void cartTest() {
        System.out.println("\nTesting Tesla!\n-------------------");
        System.out.println(tesla.getVehicleType());
        tesla.startEngine();

        System.out.println("\nTesting Mercedes!\n-------------------");
        System.out.println(mercedes.getVehicleType());
        mercedes.startEngine();

        System.out.println("\nTesting Porsche!\n-------------------");
        System.out.println(porsche.getVehicleType());
        porsche.startEngine();
    }
}

Now, let’s check the output. As you can see each startEngine() method behaves differently and this is polymorphism! :-)

polymorphism in java with examples

GitHub Project

https://github.com/swtestacademy/java-functional/tree/main/src/test/java/basics/polymorphism/interfacepolymorphism

Thanks for reading,
Onur Baskirt

2 thoughts on “Polymorphism in Java with Examples”

  1. Eline, emeğine, kalemine sağlık Onur.. Yalın ve öz anlatımın sayesinde kafamda oturtamadığım bu konuyu hızlıca kavradım.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.