Polymorphism Power

Overloading, Overriding, and

Real-World Uses

Learning Outcome(Slide2)

5

Apply polymorphism in Java programs

4

Explain method overriding concept

3

Explain method overloading concept

2

Distinguish compile and runtime polymorphism

1

Understand concept of polymorphism

Classes and Objects – How to create objects and use class methods.
 

Inheritance – How child classes inherit from parent classes.
 

Methods – How to declare and call methods, with or without parameters.

 

Compile-time vs Run-time – Difference between decisions made by the compiler and by the program at runtime.
 

super and this keywords – Calling parent class methods or constructors.

Before Starting ,Lets Recall

Think of a Person

In real life, one person plays different roles in different situations.

He is a father at home, an employee at the workplace, and a customer in a shop.

Although the person remains the same,
their behavior changes based on the role and situation.

In the same way, in Java programming,
an object can take different forms and perform different behaviors when it is used in different contexts.

Even though the reference remains the same,
the actual behavior depends on the object it refers to at runtime.

This concept—where the same method call results in different actions—is known as polymorphism in Java.

Polymorphism means “many forms.”

  • One method name, but different behaviors.
  • The same action works differently for different objects.

What is Polymorphism in Java?

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

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();  // Polymorphism
        a.sound();             // Dog barks
    }
}

Example

Method Overloading

Compile-Time Polymorphism

Static binding

When: Decision made by compiler at compile time

Rule: Same method name, different parameters.

Logic: The compiler picks the method based on arguments passed

Types of Polymorphism

class Baker{
    void bake(){
        System.out.println("Basic cake");
    }

    void bake(String flavor){
        System.out.println("Baking "+flavor);
    }

    void bake(String flavor,int size){
        System.out.println(size+"kg "+flavor);
    }
}

Here, bake() works in different ways based on input.

Method Overloading

Example:

Method Overriding

Run-Time Polymorphism

Dynamic binding

When: Decision made by JVM at run time

Rule: Same method name & parameters, but defined in child class

Logic: The behavior depends on the actual object created

Types of Polymorphism

Example:

class Cake{
    void bake(){
        System.out.println("Baking generic cake");
    }
}

class ChocolateCake extends Cake{
    @Override
    void bake(){
        System.out.println("Baking chocolate cake");
    }

The correct bake() runs based on the object.

Method Overriding

Rules of Method Overriding

Signature Match: Method name, return type, and parameters must be exactly the same.

Access Level: Cannot be more restrictive than the parent (e.g., protected ➔ public is OK, but not private).

Inheritance Only: Only inherited methods can be overridden.

No Constructors: Constructors cannot be overridden (they are not inherited).

Features

Early Binding

Late Binding

Decided at Compile Time

Decided at Run Time

Method Overloading

Method Overriding

Based on method parameters

Based on actual object type

Faster execution

More flexible

Not allowed

Allowed

Interface

Abstract Class

Sets rules for classes

Share code + enforce rules

Abstract, default, static

Abstract or concrete

Multiple (implements many)

Single (extends one)

Constants only

Any type of variable

Not allowed

Allowed

Interface

Abstract Class

Sets rules for classes

Share code + enforce rules

Abstract, default, static

Abstract or concrete

Multiple (implements many)

Single (extends one)

Constants only

Any type of variable

Not allowed

Allowed

Timing

Mechanism

Basis

Advantage

Constructor

Early binding  

Late binding

Real world applications

Debit Card

Deducts directly from bank

Credit Card

Charges credits

UPI / Wallet

Via linked virtual address

Bank Payment System

Single interface, multiple payment modes

Phone Call Button

Same action, different connection type

Calling Mom
​Via Cellular Network

Calling Office

Via Office Extension line

WhatsApp Calling

Connects via Internet

public void call(Contact c)

public void pay(double amount)

public void pay(double amount)

public void pay(double amount)

Summary

5

Object type decides method call

4

Compile-time and runtime polymorphism types

3

Method overriding changes child behavior

2

Method overloading uses different parameters

1

Polymorphism means many forms

Quiz

Which concept allows the same method to behave differently?

A.  Inheritance

B.  Encapsulation

C.  Polymorphism

D. Abstraction

Which concept allows the same method to behave differently?

A.  Inheritance

B.  Encapsulation

C.  Polymorphism

D. Abstraction

Quiz-Answer

Java - Polymorphism

By Content ITV

Java - Polymorphism

  • 12