The Four Pillars of Java - Part 1 - Polymorphism

Polymorphism is one of the fundamental concepts in object-oriented programming and is a key feature of the Java programming language. It allows objects of different classes to be treated as objects of a common superclass. 


 

In Java, polymorphism is primarily achieved through method overriding and interface implementations. Here's a more detailed explanation of polymorphism in Java:

1. Method Overriding: Polymorphism in Java is often associated with method overriding. When a subclass provides a specific implementation of a method that is already defined in its superclass, it is said to override that method. The overridden method in the subclass should have the same name, return type, and parameters as the method in the superclass. The overridden method in the subclass is then invoked instead of the superclass method when an object of the subclass is used.

   Example:

   class Animal {
       void makeSound() {
           System.out.println("Animal makes a sound");
       }
   }

   class Dog extends Animal {
       @Override
       void makeSound() {
           System.out.println("Dog barks");
       }
   }
   ```

   In the example above, if you have an instance of `Animal` or `Dog`, you can call the `makeSound` method, and Java will execute the appropriate version of the method based on the actual object type.

2. Interface Polymorphism: Java also supports polymorphism through interfaces. Interfaces define a contract that classes can implement. When a class implements an interface, it must provide implementations for all the methods declared in that interface. Then, objects of the implementing class can be treated as instances of the interface, allowing for polymorphic behavior.

   Example:

   interface Shape {
       double calculateArea();
   }

   class Circle implements Shape {
       private double radius;

       Circle(double radius) {
           this.radius = radius;
       }

       @Override
       public double calculateArea() {
           return Math.PI * radius * radius;
       }
   }

In this example, you can create objects of `Circle` and treat them as `Shape` objects, allowing you to call the `calculateArea` method in a polymorphic manner.

3. Polymorphic Method Invocation: Polymorphism enables you to write more flexible and extensible code. You can write code that works with a superclass or an interface, and at runtime, the appropriate subclass implementation will be invoked based on the actual object's type.

   Example:

   Shape shape1 = new Circle(5.0);
   Shape shape2 = new Square(4.0);

   double area1 = shape1.calculateArea(); // Calls the Circle's calculateArea
   double area2 = shape2.calculateArea(); // Calls the Square's calculateArea
   ```
In this example, you don't need to know the specific types of `shape1` and `shape2` at compile time; you can work with them polymorphically using the `Shape` interface.

Polymorphism is a powerful concept in Java that enhances code reusability, flexibility, and extensibility. It allows you to write code that can work with different types of objects, making your programs more adaptable and easier to maintain.

Comments