Polymorphism in Java3 min read

What is Polymorphism in Java?

  • Polymorphism in Java can be defined as the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
  • Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object. It is important to know that the only possible way to access an object is through a reference variable.
  • A reference variable can be of only one type. Once declared, the type of a reference variable cannot be changed. The reference variable can be reassigned to other objects provided that it is not declared final. The type of the reference variable would determine the methods that it can invoke on the object. A reference variable can refer to any object of its declared type or any subtype of its declared type. A reference variable can be declared as a class or interface type.

For example, think of a superclass called Animal that has a method called animalSound(). Subclasses of Animals could be Pigs, Cats, Dogs, Birds – And they also have their own implementation of an animal sound (the pig oinks, and the cat meows, etc.):

class Animal {
  public void animalSound() {
    System.out.println("The animal makes a sound");
  }
}

class Pig extends Animal {
  public void animalSound() {
    System.out.println("The pig says: wee wee");
  }
}

class Dog extends Animal {
  public void animalSound() {
    System.out.println("The dog says: bow wow");
  }
}

class Main {
  public static void main(String[] args) {
    Animal myAnimal = new Animal();  // Create a Animal object
    Animal myPig = new Pig();  // Create a Pig object
    Animal myDog = new Dog();  // Create a Dog object
    myAnimal.animalSound();
    myPig.animalSound();
    myDog.animalSound();
  }
}

Real-Life Example of Polymorphism

A person might have a variety of relationships with different persons. A man can be a father, a son, a brother, and a friend all at once, assuming different roles in different settings.

Types of Polymorphism in Java

  • Compile-time Polymorphism
  • Runtime Polymorphism

Compile-time polymorphism

Static polymorphism is another name for it. Method overloading is used to achieve this form of polymorphism. When many methods with the same name but different parameters exist, they are said to be overloaded. Changes in the number of arguments or the kind of arguments can cause functions to become overloaded and this is known as method overloading.

If you want to learn about method overloading in detail, click here.

Runtime polymorphism

  • It is the mechanism by which a call to an overridden function is resolved at a run time. This is also called dynamic method dispatch.
  • A superclass reference variable can refer to a sub-class object.
  • When an overridden method is called through a reference, based upon the type of object being referred to, java determines which version of that method to execute. Method Overriding is used to achieve this form of polymorphism.
If you want to learn about method Overriding in detail click here.

Share:

Leave a Reply