Skill Level: Beginner

This recipe will demonstrate how polymorphism works using the Java™ language. There are videos and step-by-step instructions for building this recipe. When you are done, you will be able to use the Java language to implement polymorphism in your projects!



  1. Let's setup your development environment

    You will need the JDK and Eclipse IDE installed to complete this recipe. I’ll assume you have some basic Java knowledge. If not, please check out the Intro to Java Programming learning path here at IBM developerWorks.

    You will also need a working development environment. If you already have a Java development environment, skip to Step 2.

    Otherwise, check out the Introduction to Java Programming learning path, Unit 2 for step-by-step instructions. There are videos in this section as well that can assist you if you need more help.

    First, download and install the Java Development Kit (JDK), version 8, and install it on your machine. Please refer to the video below if you need help.

    Next, install the Eclipse IDE to your computer. Please refer to the video below if you need help.

    Once you have your development environment setup and ready to go, you can proceed to Step 2, where you will learn about Polymorphism.

  2. Let's define polymorphism

    Polymorphism is the concept of object-oriented programming that means “different forms”, and in practical terms means that the same method name behaves differently depending on the object that implements the method.

    In the context of the Java programming language, polymorphism means implementing the same behavioral contract (via a method) differently for different objects. There are three ways we normally do this in Java:

    • interfaces
    • abstract methods
    • method overriding

    Polymorphism via interfaces

    By its very nature, an interface requires each top-level implementation to be different (by top-level, I mean the first object in the inheritance graph to explicitly state that it implements said interface). Polymorphism is achieved when an object B that implements the interface A is passed, not through through a reference to B, but rather through a reference to the interface A.

    Polymorphism via abstract methods

    The Java language also provides the ability to create an abstract method, which can exist only on an abstract class (i.e., a class that cannot be instantiated). The idea is that an abstract method, say foo(), defined on an abstract class A, must be implemented by a subclass B somewhere in the hierarchy before B can be instantiated. Polymorphism is achieved when an object of type B is passed through a reference to A.

    Polymorphism via method overriding

    When a subclass B provides an alternate implementation of a method, say foo(), in a parent class A, we say that foo() has been overridden. When an object of type B is passed to a method as A, then the Java runtime determines it must actually call B.foo(), rather than A.foo(). This is how polymorphism is achieved through method overriding.


  3. Let's write some code!

    Now that you have your development environment setup, and a basic understanding of Polymorphism, it’s time to get your hands dirty!

    In this section, you will pull the code from Github, create a new project in Eclipse, import the code, and work through the code.

    I am a big fan of Test-Driven Development (TDD) so you will start with the unit tests to see how the application should behave, and then implement the behavior until the unit tests pass. I have provided a solution to each module you must code in the com.makotojava.learn.polymorpher.solution package in case you get stuck, or just want to see how I implemented the solution.

    In this video, I show you how:

    High-level steps from the video (I highly encourage you to follow along with the video):

    • Clone the Github repo.
    • Open Eclipse.
    • Import the new Maven project based on the code in Github.
    • Run the unit tests, and watch them fail.
    • Study the unit tests to see how the methods should be written.
    • Write the implementation code until the unit tests pass.
    • Congratulations, you have written a polymorphic application!
  4. Let's put it all together

    So, what does this application demonstrate?

    As I said in Step 2, polymorphism is achieved using mainly three approaches in the Java language:

    • Through the use of interfaces
    • Through the use of abstract methods
    • Through the use of method overriding

    The unit tests tell the tale. If you study them, and really understand how they work (and why), then you will be well on your way to mastering polymorphism!


  5. Let's see what to do next

    Now that you have seen polymorphism in action, you can learn more about the Java language by taking the Intro to Java Programming learning path.

    This recipe loops from the course at Units 3-6. Check out Unit 7 to continue your journey!

    I hope you enjoyed this recipe!

    Please follow me on Twitter and send me a tweet (@jstevenperry) if you have questions, or just want to say hey!

2 comments on"Java Language Polymorphism"

  1. Two off-topic question
    1) Which tool to draw UML Diagram?

    2) Why do you prefer eclipses to NetBeans?

Join The Discussion