Lesson 3
Polymorphism in Java
Introduction

Greetings! In today's lesson, we'll unravel the concept of polymorphism in Java's Object-Oriented Programming (OOP). Grasping polymorphism enables us to use a single entity (a method, class, or interface) to represent different types in various scenarios. Let's proceed.

Seeing Polymorphism in Action

Polymorphism, a pillar of OOP, allows one object to embody multiple forms. Visualize a button in software; depending on its type (for instance, a submit button or a radio button), the action resulting from pressing it varies. This dynamic encapsulates the spirit of polymorphism!

Java supports various types of polymorphism. Run-time polymorphism, also known as dynamic polymorphism, occurs during runtime and leverages method overriding. Let's observe dynamic polymorphism in action within a simple application involving shapes. The base Shape class has an area method, which calculates the area for shapes. This method is uniquely implemented in the subclasses Rectangle and Circle.

Java
1abstract class Shape { 2 abstract double area(); 3} 4 5class Rectangle extends Shape { 6 private double length; 7 private double width; 8 9 public Rectangle(double length, double width) { 10 this.length = length; 11 this.width = width; 12 } 13 14 @Override 15 double area() { 16 return length * width; 17 } 18} 19 20class Circle extends Shape { 21 private double radius; 22 23 public Circle(double radius) { 24 this.radius = radius; 25 } 26 27 @Override 28 double area() { 29 return 3.14 * radius * radius; 30 } 31} 32 33public class Main { 34 public static void main(String[] args) { 35 Shape rectangle = new Rectangle(2, 3); 36 System.out.println(rectangle.area()); // Prints: 6.0 37 38 Shape circle = new Circle(5); 39 System.out.println(circle.area()); // Prints: 78.5 40 } 41}

Here, polymorphism shines as the area() method takes on multiple forms while using the same Shape abstract class. It behaves differently depending on whether the object is a Rectangle or a Circle.

Method Overloading in Java: Static Polymorphism

The other type of polymorphism that Java supports is a compile-time polymorphism, also known as static polymorphism, which operates during compile time and involves method overloading. Let's look at the example of the static polymorphism:

Java
1class MathOperations { 2 // Overloaded method for rectangle area 3 public double area(double length, double width) { 4 return length * width; 5 } 6 7 // Overloaded method for circle area 8 public double area(double radius) { 9 return 3.14 * radius * radius; 10 } 11} 12 13public class Main { 14 public static void main(String[] args) { 15 MathOperations mathOps = new MathOperations(); 16 System.out.println(mathOps.area(5, 2)); // Rectangle area 17 System.out.println(mathOps.area(3)); // Circle area 18 } 19}

In this example, polymorphism is demonstrated via method overloading. The area method has multiple forms, accepting different parameter lists to compute the area of either a rectangle or a circle based on the provided arguments. This is a clear example of static polymorphism, where the correct method is determined at compile time.

Lesson Summary

Great job! We've now learned about polymorphism, observed its implementation, and discovered its applications. Now, prepare for hands-on practice tasks. Apply what you've learned and excel in Java programming. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.