Login
Password

Forgot your password?

Understanding Java: Polymorphism

By Edited Jun 1, 2015 0 0

Polymorphism is one of the key ideas in Object Oriented programming. The basic idea is that one thing or in this case object can come in several different forms. In Java polymorphism comes in three different forms – Overriding, Overloading, and Dynamic Method Binding. Below we will cover each of these forms in detail using examples to help you to better understand how polymorphism works in Java.

Overriding

In Java overriding is when a method of a subclass has the same method signature as the superclass. In this instance when the method is called the subclass one is executed and is said to “override” the superclass one. Overriding can be useful as you can see below when different instances of related classes need to perform slightly different actions.

public class Person {
    public void printGreeting() {
        System.out.println(“Hello I am a person”);
    }
}

public class Student extends Person {
    public void printGreeting() {
        System.out.println(“Hello I am a student”);
    }
}

public class Test {    
    public static void main(String[] args) {
        Person person1 = new Person();
        Person person2 = new Student();
        person1.printGreeting();
        person2.printGreeting();
    }
}

If you run the above example you will see that the output is “Hello I am a person”, “Hello I am a student”. The printGreeting method in the Student class is overriding the printGreeting method in the Person class which results in the students method being invoked for person2 at runtime.

Overloading

Overloading in Java is when you have several methods all with the same method name and taking differing types of parameters. Overloading is common in Java and some common examples can be seen in the JDK itself:

Math.round(double)

Math.round(float)

The round method of the Math class can take either a double or a float. Which method is invoked will depend on the type of the parameter passed in.

 

String.indexOf(int)
String.indexOf(int, int)
String.indexOf(String)
String.indexOf(String, int)

The indexOf method of the String class takes either 1 or 2 parameters and the type of the first parameter can be either an integer or a string.

 

Something interesting to note here is that the return type of the method is not taken into account when looking at the method signature for overloading thus the following examples are invalid and would cause a compile time error:

 

public boolean tryMe() {
    return true;
}

and

public int tryMe() {
    return 0;
}

Dynamic Method Binding

The final type of polymorphism is Dynamic Method Binding. This is sometimes also referred to as Late Method Binding. To explain this lets first look at an example and then describe what is happening.

 

public abstract class Shape {
    public abstract void printType();
}

public class Circle extends Shape {
    public void printType() {
        System.out.println(“Circle”);
    }
}

public class Triangle extends Shape {
    public void printType() {
        System.out.println(“Triangle”);
    }
}

public class Test {
    public static void main(String[] args) {
        Shape shape;
        Circle circle = new Circle();
        Triangle triangle = new Trianle();
        shape = circle;
        shape.printType();
        shape = triangle;
        shape.printType();
    }
}

In this example we start with an abstract class. An abstract class is a class that represents a concept (in this case a shape) but we require specific subclasses to complete the implementation by implementing the abstract methods and thus defining specific behaviour for each concrete class.

At compile time the compiler isn't sure what class the shape variable will be referencing so the method invocation is dynamically figured out at runtime. When the shape variable references the Circle object it invokes the method on the circle object. When the shape variable references the Triangle object it invokes the method on the triangle object.

Advertisement

Comments

Add a new comment - No HTML
You must be logged in and verified to post a comment. Please log in or sign up to comment.

Explore InfoBarrel

Auto Business & Money Entertainment Environment Health History Home & Garden InfoBarrel University Lifestyle Sports Technology Travel & Places
© Copyright 2008 - 2016 by Hinzie Media Inc. Terms of Service Privacy Policy XML Sitemap

Follow IB Technology