OOP_3| Polymorphism IN Java

Nikita pandey
4 min readFeb 21, 2023

Polymorphism is a concept in object-oriented programming that allows you to use a single interface to represent multiple types of objects.

{Same name with different forms is the concept of polymorphism.}

  1. Compile-Time Polymorphism Compile-time polymorphism is achieved through method overloading, which allows you to define multiple methods with the same name but different parameters in the same class.
public class Calculator {
public int add(int a, int b) {
return a + b;
}

public double add(double a, double b) {
return a + b;
}
}

In the above example, we have a class called “Calculator” with two methods named “add”. One takes two integers as arguments, and the other takes two doubles. We can call the appropriate version of the “add” method based on the types of the arguments passed in.

2. Run-Time Polymorphism is achieved through method overriding, which allows you to provide a different implementation of a method that is already defined in a superclass or interface.

Example code:

public interface Animal {
public void speak();
}

public class Dog implements Animal {
public void speak() {
System.out.println("Woof!");
}
}

public class Cat implements Animal {
public void speak() {
System.out.println("Meow!");
}
}

In the above example, we have an interface called “Animal” with a method named “speak”. We have two classes that implement the “Animal” interface — “Dog” and “Cat”. Each class provides its own implementation of the “speak” method. When we call the “speak” method on an object of type “Animal”, the appropriate version of the method (either “Dog” or “Cat”) will be executed depending on the actual type of the object.

EARLY BINDING V/s LATE BINDING

Early binding is also known as static binding or compile-time binding. It occurs when the compiler can determine which method to call at compile-time based on the type of the reference variable. In other words, the method that will be called is determined at the time the program is compiled. This means that the method that will be executed is known at the compile-time and is fixed.

On the other hand, late binding is also known as dynamic binding or runtime binding. It occurs when the compiler cannot determine which method to call at compile-time because the type of the object is not known until the program is run. In this case, the method that will be called is determined at the time the program is run. This means that the method that will be executed is determined at runtime and may vary based on the type of the object that is being referred to.

class Animal {
public void makeSound() {
System.out.println("Generic animal sound");
}
}

class Cat extends Animal {
public void makeSound() {
System.out.println("Meow");
}
}

class Dog extends Animal {
public void makeSound() {
System.out.println("Woof");
}
}

public class Main {
public static void main(String[] args) {
Animal animal = new Cat(); // Upcasting
animal.makeSound(); // Early binding, calls Cat's makeSound() method
animal = new Dog(); // Upcasting
animal.makeSound(); // Early binding, calls Dog's makeSound() method
}
}

In this example, we have a class hierarchy where Cat and Dog are both subclasses of Animal. In the Main class, we first create an instance of Cat and assign it to an Animal reference variable called animal. When we call the makeSound() method on animal, it will call the makeSound() method defined in the Cat class, because the reference variable is of type Animal, but the object being referred to is of type Cat.

Later, we assign a Dog object to the same animal reference variable and call the makeSound() method again. This time, it will call the makeSound() method defined in the Dog class, because the reference variable is still of type Animal, but the object being referred to is of type Dog

In both cases, we are using early binding because the compiler can determine which method to call at compile-time based on the type of the reference variable.

public class Main {
public static void main(String[] args) {
Animal animal;
animal = getRandomAnimal(); // This method returns either a Cat or a Dog object
animal.makeSound(); // Late binding, calls either Cat's or Dog's makeSound() method
}

public static Animal getRandomAnimal() {
if (Math.random() < 0.5) {
return new Cat();
} else {
return new Dog();
}
}
}

In this example, we have a method called getRandomAnimal() that returns either a Cat or a Dog object at random. In the Main class, we call this method and assign the result to an Animal reference variable called animal. Then, we call the makeSound() method on animal.

Photo by Michiel Leunens on Unsplash

Conclusion Polymorphism is a powerful and flexible concept that allows you to write code that is more modular, reusable, and extensible. By using a single interface to represent multiple types of objects, you can write code that is more generic and adaptable, and that can work with new types of objects without needing to be modified.

--

--

Nikita pandey

Python developer| Data science student| detective by nature| into art,AI, and books