Unit 9: Inheritance

Polymorphism in Java

Introduction to Polymorphism

Polymorphism, a foundational concept in object-oriented programming, allows objects of different classes to be treated as objects of a common super class. The term "polymorphism" is derived from Greek, meaning "many shapes", and in Java, it enables one interface to be used for a general class of actions.


Pillars of Polymorphism

What is Polymorphism?

Polymorphism allows Java objects to share a common interface or extend a class to provide specialized behavior. It's one of the four principal concepts of object-oriented programming, alongside encapsulation, inheritance, and abstraction.

Types of Polymorphism

  1. Compile-time Polymorphism (Method Overloading):

    • Occurs when two or more methods in the same class have the same name but different parameters.
    • Its usage is determined at compile-time.
    void print(int a) {
        System.out.println(a);
    }
    
    void print(double a) {
        System.out.println(a);
    }
    
  2. Runtime Polymorphism (Method Overriding):

    • Occurs when a subclass provides a specific implementation of a method that is already defined in its superclass.
    • Its usage is determined at runtime based on the object used to call the method.
    class Animal {
        void sound() {
            System.out.println("Animal makes a sound");
        }
    }
    
    class Dog extends Animal {
        @Override
        void sound() {
            System.out.println("Dog barks");
        }
    }
    

Remember

While method overloading is a compile-time polymorphism, method overriding is runtime polymorphism. The difference lies in when the exact method to be called is determined.

Benefits of Polymorphism

  • Flexibility: Allows objects of different classes to be treated as instances of the same class.
  • Extensibility: Provides a mechanism to add new classes without modifying existing ones.
  • Interchangeability: Objects of different classes can be swapped and can still offer consistent behavior.

Delving Deeper: Real-world Analogy

Imagine a universal remote control that can operate various devices: a TV, a DVD player, an air conditioner, etc. Each device has its distinct behavior for the "power" button (TV turns on/off, DVD player opens/closes the tray, air conditioner starts/stops cooling). The remote, however, sees just the "power" action. This is polymorphism: one interface, multiple implementations.


Summary

Polymorphism in Java introduces flexibility into the code, allowing one interface to have multiple implementations. It's an essential concept in object-oriented programming that promotes reusability and can be seen in everyday examples. Mastering polymorphism is crucial for anyone aiming for excellence in AP Computer Science A.


References


AP CSA Homework Assignment

Assignment: Shapes and Polymorphism

Instructions

  1. Create an abstract class Shape with a method area() that returns a double.
  2. Create two subclasses, Rectangle and Circle, that extend Shape.
  3. Override the area() method in both subclasses to compute and return the area for each shape.
  4. In a main method, create an array of Shape and initialize it with some Rectangle and Circle objects.
  5. Traverse the array and print the area of each shape.

This assignment will let you experience polymorphism firsthand by treating different shapes as general shapes and computing their areas through a common interface.

Previous
Overriding Methods