“The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages. This principle can also be applied to object-oriented programming and languages like the Java language. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.” – Oracle

What is polymorphism in Java object-oriented programming?

Sometimes, the terminology, make thing abstruse than it really is…

Basically, polymorphism allows a method of a class works in different ways depends on which sub-class the object initiated from.

To understand, we will consider an example that I called “What does the fox say”:

First, we define a superclass Animal. Every animal species have its own name and sound.

class Animal {
    private String name;

    public Animal(String name) {
        this.name = name;
    }

    public String saySomething() {
        return "remain unknown";
    }

    public String getName() {
        return name;
    }
}

Now, we will make some subclasses that extend from that superclass, in other words, to define other species bases on the attributes and behaviors of all kind of animal.

Polymorphism example:

class Cat extends Animal {
    public Cat(String name) {
        super(name);
    }

    public String saySomething() {
        return "Meow meow";
    }
}

class Dog extends Animal {
    public Dog (String name) {
        super(name);
    }

    @Override
    public String saySomething() {
        return "Woof woof";
    }
}


class Fox extends Animal {
    public Fox(String name) {
        super(name);
    }
}
Dog goes "woof"
Cat goes "meow"

Everyone knows it, right. That’s why we can re-defined the saySomething() method and return what those animal say.

But there's one sound
That no one knows
What does the fox say?

Sadly, no one knows what the fox says, so I can’t re-defined the method saySomething().

How Polymorphism works

Let’s initiate the real object of those subclass of Animal and see how Polymorphism works

public static void main(String[] args) {
    Animal cat = new Cat("Tom");
    Animal dog = new Dog("Scoppy Do");
    Animal fox = new Fox("MR. Fox");


    System.out.println(nameAndSay(cat));
    System.out.println(nameAndSay(dog));
    System.out.println(nameAndSay(fox));
}

public static String nameAndSay(Animal animal){
    return animal.getName() + " say : "+ animal.saySomething();
}

Output:

Tom say : Meow meow
Scoppy Do say : Woof woof
MR. Fox say : remain unknown

As you can see, we call the same method saySomething() for each of the 3 instances and each of them will have distinct responses for the same behavior base on the re-defined method that we make inside subclass.

Or, that behavior of Animal will be different from species to species.

Since nobody has ever heard the fox say, the class Fox inherited the method saySomething() from the superclass of Animal. Thus, return “remain unknown”.

One of the advantages of Polymorphism is you don’t have to define so many method name for the same behavior such as: catSaySomething(), dogSaySomething(), cowSaySomething(), foxWontSayAnything().

“The Java virtual machine (JVM) calls the appropriate method for the object that is referred to in each variable. It does not call the method that is defined by the variable’s type. This behavior is referred to as virtual method invocation and demonstrates an aspect of the important polymorphism features in the Java language.” —  Oracle

 

 

 

 

Click for source codes
public class Poly {

    public static void main(String[] args) {
        Animal cat = new Cat("Tom");
        Animal dog = new Dog("Scoppy Do");
        Animal fox = new Fox("MR. Fox");


        System.out.println(nameAndSay(cat));
        System.out.println(nameAndSay(dog));
        System.out.println(nameAndSay(fox));
    }

    public static String nameAndSay(Animal animal){
        return animal.getName() + " say : "+ animal.saySomething();
    }


}
class Animal {
    private String name;

    public Animal(String name) {
        this.name = name;
    }

    public String saySomething() {
        return "remain unknown";
    }

    public String getName() {
        return name;
    }
}

class Cat extends Animal {
    public Cat(String name) {
        super(name);
    }

    public String saySomething() {
        return "Meow meow";
    }
}

class Dog extends Animal {
    public Dog (String name) {
        super(name);
    }

    @Override
    public String saySomething() {
        return "Woof woof";
    }
}


class Fox extends Animal {
    public Fox(String name) {
        super(name);
    }
}

 

 

 

 

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here