Polymorphism is a fundamental concept in Object-Oriented Programming (OOP), allowing objects of different classes to be treated as objects of a common superclass. It is the ability of an object to take on multiple forms, enabling a single method to behave differently based on the object’s class.
Polymorphism in Java means "many shapes," and it allows one interface to be used for a general class of actions. The specific action is determined by the exact nature of the situation. Polymorphism can be classified into two types:
Polymorphism enhances the flexibility and extensibility of the code. It allows developers to write more generic code, which works with objects of different types, thus reducing complexity and increasing reusability.
There are two types of polymorphism in Java:
Method overloading is an example of compile-time polymorphism, where two or more methods in the same class share the same name but differ in the number or type of parameters.
class Calculator { // Method overloading: same method name but different parameters int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } } public class Main { public static void main(String[] args) { Calculator calc = new Calculator(); System.out.println(calc.add(5, 10)); // Calls add(int, int) System.out.println(calc.add(5.5, 10.5)); // Calls add(double, double) } }
In this example, the add()
method is overloaded. Depending on the type of arguments passed, the appropriate method version is called at compile time.
Method overriding is an example of runtime polymorphism. It occurs when a subclass provides a specific implementation of a method already defined in its superclass.
class Animal { void sound() { System.out.println("Animals make a sound."); } } class Dog extends Animal { @Override void sound() { System.out.println("The dog barks."); } } public class Main { public static void main(String[] args) { Animal animal = new Dog(); // Animal reference, Dog object animal.sound(); // Calls Dog's overridden sound() method } }
In this example, the sound()
method is overridden in the Dog class. Despite the reference being of type Animal, the Dog class's method is invoked, demonstrating runtime polymorphism.
Polymorphism works by allowing one interface to be used for different underlying forms (data types). It enables the same method or class to perform different tasks based on the object it is called on.
Consider the following scenario:
class Animal { void makeSound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { void makeSound() { System.out.println("Dog barks"); } } class Cat extends Animal { void makeSound() { System.out.println("Cat meows"); } } public class Main { public static void main(String[] args) { Animal myAnimal = new Animal(); myAnimal.makeSound(); // Animal makes a sound myAnimal = new Dog(); myAnimal.makeSound(); // Dog barks myAnimal = new Cat(); myAnimal.makeSound(); // Cat meows } }
In this case, the makeSound()
method demonstrates polymorphism. Even though the reference is of type Animal, the appropriate Dog or Cat class method is invoked based on the object being referenced, showing runtime polymorphism.
In Java, interfaces are also a form of polymorphism. When a class implements an interface, it can override the methods of that interface. This allows different classes to implement the same interface and be treated uniformly.
interface Animal { void makeSound(); } class Dog implements Animal { public void makeSound() { System.out.println("Dog barks"); } } class Cat implements Animal { public void makeSound() { System.out.println("Cat meows"); } } public class Main { public static void main(String[] args) { Animal myDog = new Dog(); myDog.makeSound(); // Dog barks Animal myCat = new Cat(); myCat.makeSound(); // Cat meows } }
In this case, both Dog and Cat implement the Animal interface, and we can treat both as Animal types, demonstrating polymorphism with interfaces.
Polymorphism is a powerful feature of Java that allows methods and objects to behave in different ways based on their actual class. It promotes flexibility, reusability, and maintainability in object-oriented programming. Whether through method overloading (compile-time polymorphism) or method overriding (runtime polymorphism), polymorphism enhances code efficiency and design simplicity.
Quick Tip:
Remember, polymorphism allows you to invoke the same method on objects of different types, providing flexibility in how your code interacts with those objects.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!