Polymorphism in Java
Objectives
- Understand the concept of polymorphism
- Differentiate between method overloading and method overriding
- Utilize the super keyword in Java
- Grasp runtime polymorphism in Java
- Learn about abstraction in Java, abstract classes, and interfaces
What is Polymorphism?
- Polymorphism refers to the capability of different objects to respond to the same method call according to their type-specific behavior.
- It enables objects of different classes to be treated as objects of a common super class.
- Derived from the Greek words "poly" (many) and "morphs" (forms), meaning many forms.
- In Java, polymorphism allows one action to be executed in multiple ways.
Implementing Polymorphism
1. Method Overloading
- Same method name used for multiple functions within the same class.
- Constructor Overloading: Having multiple constructors in the same class.
- Enhances code readability.
- Types of Method Overloading:
- By changing the number of arguments
- By changing the data type of arguments
Example of Method Overloading
- Changing number of arguments
class Adder {
static int add(int a, int b) { return a + b; }
static int add(int a, int b, int c) { return a + b + c; }
}
- Changing data type of arguments
class Adder {
static int add(int a, int b) { return a + b; }
static double add(double a, double b) { return a + b; }
}
- Key Points: Overloading is not allowed merely by changing the return type because it leads to ambiguity.
2. Method Overriding
- Redefining a method in a subclass that already exists in the parent class.
- When a child class provides a specific implementation of a method, it overrides the method of the parent class.
- Method name must be the same as in the parent class.
- Parameter list must be identical to the parent class.
- An IS-A relationship (inheritance) must exist between parent and child.
Example of Method Overriding
// Parent class
class Vehicle {
void run() { System.out.println("Vehicle is running"); }
}
// Child class
class Bike extends Vehicle {
void run() { System.out.println("Bike is running safely"); }
public static void main(String args[]) {
Bike obj = new Bike();
obj.run();
}
}
Super Keyword in Java
super: Reference variable to refer to the parent class's object. - Used:
- To access parent class instance variables
- To invoke parent class methods
- To call the parent class constructor
Example Usage of Super Keyword
- Accessing parent's variable:
class Animal {
String color = "white";
}
class Dog extends Animal {
String color = "black";
void printColor() {
System.out.println(color); // Dog's color
System.out.println(super.color); // Animal's color
}
}
- Invoking parent class methods:
class Animal {
void eat() { System.out.println("eating..."); }
}
class Dog extends Animal {
void eat() { System.out.println("eating bread..."); }
void work() {
super.eat(); // Call parent class method
bark();
}
}
Runtime Polymorphism
- Runtime or Dynamic Method Dispatch occurs when a call to an overridden method is resolved at runtime.
- Upcasting is when the reference variable of a parent class refers to an object of a child class.
Example of Runtime Polymorphism
class Bike {
void run() { System.out.println("running"); }
}
class Splendor extends Bike {
void run() { System.out.println("running safely with 60km"); }
public static void main(String args[]) {
Bike b = new Splendor(); // Upcasting
b.run();
}
}
Abstraction in Java
- Abstraction: The process of hiding implementation details and showing only functionality.
- Achieved via abstract classes and interfaces.
Abstract Classes
- Declared with the
abstract keyword. - Cannot be instantiated but can have both abstract and non-abstract methods.
Interfaces
- A contract that a class must follow.
- Contains only abstract methods and static constants.
- A class that implements an interface must define all of its methods.
Multiple Inheritance through Interfaces
- If a class implements multiple interfaces, it enables multiple inheritance.
interface Printable {
void print();
}
interface Showable {
void show();
}
class A implements Printable, Showable {
public void print() { System.out.println("Hello"); }
public void show() { System.out.println("Welcome"); }
}