Sitemap
Aman Verma

Aman Verma

📚#Software Engineer || Ai Technolgy || #ReadingIsLife || #techylife https://blog-project-one-silk.vercel.app/

Java Day-44

3 min readNov 26, 2025

--

Polymorphism

Press enter or click to view image in full size

Poly → many
Morphism → forms

Meaning:

Same object performing different behaviors.

In real life, one person can do many tasks.
Similarly, in Java, a method or constructor can perform many tasks.

Example:

void add(int a, int b)
void add(int a, int b, int c)
void add(float a, float b)
void add(int a, float b)

Types of Polymorphism

Polymorphism is divided into two types:

1. Static Polymorphism (Compile-time Polymorphism / Early Binding)

✔ Happens at compile time
✔ Compiler knows which method to call based on method parameters
✔ Achieved using Method Overloading

Example:

add(int, int)
add(int, int, int)
add(double, double)

Binding type: Early Binding → decided by compiler

2. Dynamic Polymorphism (Runtime Polymorphism / Late Binding)

✔ Happens at runtime
✔ Compiler does not know which method will run
✔ JVM decides method based on object type

✔ Achieved using Method Overriding

Get Aman Verma’s stories in your inbox

Join Medium for free to get updates from this writer.

Example:

class Parent {
void show() {}
}
class Child extends Parent {
void show() {}
}

Binding type: Late Binding → decided by JVM at runtime

3. Method Overloading

Definition:

Writing multiple methods with the same name in the same class, but with different parameter lists.

Difference must be in:

  1. Number of parameters
  2. Type of parameters
  3. Order of parameters

Return type does NOT matter.

Method Overloading Examples

✔ By Number of Parameters

public void display(int a) { }
public void display(int a, int b) { }

✔ By Type of Parameters

public void show(int a) { }
public void show(double a) { }

✔ By Order of Parameters

public void test(int a, double b) { }
public void test(double a, int b) { }

Example of Method Overloading (Payment System)

public void makePayment(Cash c) { }
public void makePayment(UPI c) { }
public void makePayment(CreditCard c) { }

Rules for Method Overloading

  1. Method names must be the same
  2. Must differ in number/type/order of parameters
  3. Can change the return type
  4. Access modifiers can change
  5. Static methods can be overloaded
  6. Type promotion applies (e.g., int → long → float)
  7. Cannot overload only by changing return type

Advantages of Method Overloading

  • Increases code reusability
  • The same method name can perform multiple tasks
  • Cleaner and easier code
  • Improves readability

Examples:
System.out.print() and println() → Overloaded many times for different types.

Programs from Notes

Program 1 — Overloading with Different Return Types

package com.ravi.mol;
class Addition {
public int doSum(int x, int y) {
return (x + y);
}
protected double doSum(double x, double y) {
return (x + y);
}
protected String doSum(String a, String b) {
return (a + b);
}
}
public class MOLDemo1 {
public static void main(String[] args) {
Addition addition = new Addition();
System.out.println("Sum of two integer is :" + addition.doSum(12, 24));
System.out.println("Sum of two double is :" + addition.doSum(1.2, 2.4));
System.out.println("Concatenation of two String is :" + addition.doSum("Data", "base"));
}
}

Program 2 — Overloading Static Methods

package com.ravi.mol;
record Student(int roll, String name) { }
record Employee(int id, double salary) { }
class Sample { public static void accept(Student student) {
System.out.println("Roll Number is :" + student.roll());
System.out.println("Student Name is :" + student.name());
}
public static void accept(Employee employee) {
System.out.println("Employee Id is :" + employee.id());
System.out.println("Salary is :" + employee.salary());
}
}
public class MOLDemo2 {
public static void main(String[] args) {
Sample.accept(new Employee(101, 50000));
Sample.accept(new Student(1, "Raj"));
}
}

5. Method Overriding (Quick Notes)

Method Overriding → Same method name, same parameters OR same signature
Occurs in Inheritance (super & sub class)
Used to achieve runtime polymorphism

Rules:

  1. Must be in different classes (parent-child)
  2. Method signature must be the same
  3. Access modifier of the child method must be the same or more visible
  4. Cannot override private, static, final methods
  5. Return type must be the same (or covariant)

--

--

Aman Verma

Written by Aman Verma

📚#Software Engineer || Ai Technolgy || #ReadingIsLife || #techylife https://blog-project-one-silk.vercel.app/