What is Polymorphism in Java?
Polymorphism is considered one of the important features of Object-Oriented Programming. Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations. The word “poly” means many and “morphs” means forms, So it means many forms.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | class Polygon { // method to render a shape public void render() { System.out.println("Rendering Polygon..."); } } class Square extends Polygon { // renders Square public void render() { System.out.println("Rendering Square..."); } } class Circle extends Polygon { // renders circle public void render() { System.out.println("Rendering Circle..."); } } class Main { public static void main(String[] args) { // create an object of Square Square s1 = new Square(); s1.render(); // create an object of Circle Circle c1 = new Circle(); c1.render(); } } |
Output:
1 2 | Rendering Square... Rendering Circle... |
Types of Java Polymorphism
In Java Polymorphism is mainly divided into two types:
- Static Polymorphism
- Dynamic Polymorphism
Static Polymorphism
Also known as method overloading, static polymorphism occurs when multiple methods in the same class share the same name but have different parameters (different type, number, or order).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | // Java Program for Method overloading // By using Different Types of Arguments // Class 1 // Helper class class Helper { // Method with 2 integer parameters static int Multiply(int a, int b) { // Returns product of integer numbers return a * b; } // Method 2 // With same name but with 2 double parameters static double Multiply(double a, double b) { // Returns product of double numbers return a * b; } } // Class 2 // Main class class Wrap { // Main driver method public static void main(String[] args) { // Calling method by passing // input as in arguments System.out.println(Helper.Multiply(2, 4)); System.out.println(Helper.Multiply(5.5, 6.3)); } } |
Output:
1 2 | 8 34,65 |
Dynamic Polymorphism
Also known as method overriding, dynamic polymorphism occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. The method to be called is determined at runtime.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | // Java Program for Method Overriding // Class 1 // Helper class class Parent { // Method of parent class void Print() { // Print statement System.out.println("parent class"); } } // Class 2 // Helper class class subclass1 extends Parent { // Method void Print() { System.out.println("subclass1"); } } // Class 3 // Helper class class subclass2 extends Parent { // Method void Print() { // Print statement System.out.println("subclass2"); } } // Class 4 // Main class class Wrap { // Main driver method public static void main(String[] args) { // Creating object of class 1 Parent a; // Now we will be calling print methods // inside main() method a = new subclass1(); a.Print(); a = new subclass2(); a.Print(); } } |
Output:
1 2 | subclass1 subclass2 |
Using Interfaces for Polymorphism
Interfaces in Java provide another way to achieve polymorphism. An interface defines a contract of methods that a class must implement, allowing objects to be used through the interface.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | interface Animal { void makeSound(); } class Dog implements Animal { @Override public void makeSound() { System.out.println("Dog barks"); } } class Cat implements Animal { @Override public void makeSound() { System.out.println("Cat meows"); } } public class Main { public static void main(String[] args) { Animal myDog = new Dog(); Animal myCat = new Cat(); myDog.makeSound(); // Output: Dog barks myCat.makeSound(); // Output: Cat meows } } |
Benefits of Polymorphism
- Flexibility: Polymorphism allows methods to work on objects of different classes, making the code more flexible.
- Extensibility: New functionality can be added by simply creating new classes and overriding methods without changing existing code.
- Maintainability: It is easier to maintain and update code since behavior is defined through interfaces or base classes.
Disadvantages of Polymorphism
- Can make it more difficult to understand the behavior of an object, especially if the code is complex.
- This may lead to performance issues, as polymorphic behavior may require additional computations at runtime.