Lesson 24
Polymorphism
Polymorphism allows methods and objects to take multiple forms. Covers compile-time and runtime polymorphism with examples.
Java Track
Save progress as you learn.
61 lessons
Lesson content
Polymorphism in Java is one of the core OOP concepts that allows a method, object, or class to take multiple forms. It improves code flexibility, reusability and maintainability.
What is Polymorphism in Java?
The word Polymorphism means 'many forms.' In Java, polymorphism allows the same method name or object reference to behave differently depending on the context.
Method overloading and method overriding are commonly used to achieve polymorphism.
Definition List
- Polymorphism: The ability of a method, object, or interface to take multiple forms.
- Method Overloading: Defining multiple methods with the same name but different parameter lists within the same class.
- Method Overriding: Providing a new implementation of a method that already exists in a parent class.
- Compile-Time Polymorphism: Polymorphism achieved through method overloading, resolved during compilation.
- Runtime Polymorphism: Polymorphism achieved through method overriding, resolved during program execution.
- Upcasting: Converting a child class reference to a parent class reference.
- Downcasting: Converting a parent class reference back to a child class reference.
Types of Polymorphism in Java
- Compile-Time Polymorphism (Method Overloading)
- Runtime Polymorphism (Method Overriding)
Method Overloading in Java
Method overloading means creating multiple methods with the same name but different parameter lists or data types. The compiler determines which method to execute based on the arguments passed.
Features of Method Overloading
- Same method name
- Different parameter lists
- Different number of parameters
- Different data types
- Occurs within the same class
Example 01: Method Overloading
class methodoverload {
void demo() {
System.out.println("Printing no parameter");
}
void demo(int i) {
System.out.println("Printing one parameter");
}
void demo(int i, int j) {
System.out.println("Printing two parameters");
}
}The demo() method is overloaded three times: no parameters, one parameter and two parameters. The compiler selects the correct method based on the arguments passed.
Example 02: Method Overloading with Math Operations
class MathOperation {
public static int multiply(int a, int b) {
return a * b;
}
public static double multiply(double x, double y) {
return x * y;
}
public static double multiply(double i, int j) {
return i * j;
}
public static int multiply(int r) {
return r * r;
}
}
public class methodOverloading {
public static void main(String arg[]) {
System.out.println("Multiply 2 Integer Value : " + MathOperation.multiply(25, 10));
System.out.println("Multiply 2 Double Value : " + MathOperation.multiply(2.5, 8.5));
System.out.println("Multiply Double & Integer Value : " + MathOperation.multiply(2.5, 8));
System.out.println("Multiply Integer Value : " + MathOperation.multiply(2));
}
}Output
Multiply 2 Integer Value : 250
Multiply 2 Double Value : 21.25
Multiply Double & Integer Value : 20.0
Multiply Integer Value : 4Benefits of Method Overloading
- Improves code readability
- Reduces the number of method names
- Supports compile-time polymorphism
- Makes programs easier to maintain
Constructor Overloading in Java
Constructor overloading means defining multiple constructors with different parameter lists within the same class.
Features of Constructor Overloading
- Same constructor name as the class
- Different parameter lists
- Different initialization behavior
Example 03: Constructor Overloading
class Box {
float length, breadth;
public Box() {
length = 2;
breadth = 5;
}
public Box(float x, float y) {
length = x;
breadth = y;
}
Box(float x) {
length = breadth = x;
}
float area() {
return length * breadth;
}
}
public class constructor_overloading {
public static void main(String args[]) {
Box o = new Box();
System.out.println("Area : " + o.area());
Box o1 = new Box(3, 6);
System.out.println("Area : " + o1.area());
Box o2 = new Box(3);
System.out.println("Area : " + o2.area());
}
}Output
Area : 10.0
Area : 18.0
Area : 9.0Method Overriding in Java
Method overriding occurs when a subclass provides a specific implementation of a method that already exists in the parent class. The method name, parameters and return type must remain the same.
Features of Method Overriding
- Requires inheritance
- Same method signature
- Same return type
- Runtime decision-making
- Supports runtime polymorphism
Example 04: Method Overriding
class user {
String name;
int age;
user(String n, int a) {
this.name = n;
this.age = a;
}
public void display() {
System.out.println("Name : " + name);
System.out.println("Age : " + age);
}
}
class MainProgrammer extends user {
String CompanyName;
MainProgrammer(String n, int a, String c) {
super(n, a);
this.CompanyName = c;
}
public void display() {
System.out.println("Name : " + name);
System.out.println("Age : " + age);
System.out.println("Company Name : " + CompanyName);
}
}
public class methodOverriding {
public static void main(String args[]) {
MainProgrammer o = new MainProgrammer("Raja", 22, "Tutor Joes");
o.display();
}
}Output
Name : Raja
Age : 22
Company Name : Tutor JoesBenefits of Method Overriding
- Enables runtime polymorphism
- Improves code flexibility
- Allows customization of inherited methods
- Supports dynamic method dispatch
Dynamic Polymorphism (Dynamic Method Dispatch)
Dynamic polymorphism occurs when the method to execute is determined at runtime rather than compile time. An object can take multiple forms during program execution.
Upcasting
Upcasting is the process of converting a child class reference into a parent class reference. Example: bank b = new SBI();
Downcasting
Downcasting is the process of converting a parent class reference back into a child class reference. Example: ICICI bk = (ICICI) b;
Example 05: Dynamic Dispatch
class bank {
float getRateOfInterest() { return 0; }
}
class SBI extends bank {
float getRateOfInterest() { return 8.4f; }
}
class ICICI extends bank {
float getRateOfInterest() { return 7.3f; }
}
class AXIS extends bank {
float getRateOfInterest() { return 9.7f; }
}
class DynamicDispatchExample {
public static void main(String args[]) {
bank b;
b = new SBI(); // upcasting
System.out.println("SBI Rate of Interest: " + b.getRateOfInterest());
b = new ICICI();
System.out.println("ICICI Rate of Interest: " + b.getRateOfInterest());
ICICI bk = (ICICI) b; // downcasting
System.out.println("ICICI Rate of Interest: " + bk.getRateOfInterest());
b = new AXIS();
System.out.println("AXIS Rate of Interest: " + b.getRateOfInterest());
}
}Output
SBI Rate of Interest: 8.4
ICICI Rate of Interest: 7.3
ICICI Rate of Interest: 7.3
AXIS Rate of Interest: 9.7Method Overloading vs Method Overriding
- Definition — Overloading: same name, different parameters. Overriding: method redefined in child class.
- Inheritance Required — Overloading: No. Overriding: Yes.
- Return Type — Overloading: can vary. Overriding: must be compatible.
- Binding Time — Overloading: Compile Time. Overriding: Runtime.
- Polymorphism Type — Overloading: Compile-Time. Overriding: Runtime.
Advantages of Polymorphism
- Code Reusability: The same interface can be reused for different implementations.
- Flexibility: Programs can easily adapt to new requirements.
- Maintainability: Changes can be made without affecting existing code.
- Extensibility: New functionality can be added with minimal modifications.
- Improved Readability: Developers can use common method names for related operations.
FAQ
1. What is polymorphism in Java?
Polymorphism is the ability of a method, object, or class to take multiple forms and perform different actions based on context.
2. What is the difference between method overloading and method overriding?
Method overloading uses the same method name with different parameters, while method overriding redefines a parent class method in a child class.
3. What is runtime polymorphism in Java?
Runtime polymorphism occurs when the method to execute is determined during program execution using method overriding and dynamic dispatch.
Where Java is used
Java in real-world development
Compile-Time Polymorphism
Achieved through method overloading. Compiler resolves the method call based on parameters.
Runtime Polymorphism
Achieved through method overriding. JVM resolves the method call during execution.
Method Overloading
Same method name, different parameters. No inheritance required.
Method Overriding
Child class redefines parent method. Same name, same parameters, same return type.
Upcasting
Child reference assigned to parent type. Example: bank b = new SBI();
Downcasting
Parent reference cast back to child type. Example: ICICI bk = (ICICI) b;