Q12. What is polymorphism in Java? What are the different types of polymorphism?
Explain with
example.
What are features / objectives / characteristics / properties / significance / principles of
polymorphism?
Definition:
Polymorphism in Java is the ability of a single function, method, or operator to behave differently
based on the context or data it operates on. It allows one interface to be used for a general class of
actions.
Types of Polymorphism:
1. Compile-time Polymorphism (Static Binding):
- Achieved using method overloading.
- Decision made at compile time.
Example:
class MathOperation {
int add(int a, int b) {
return a + b;
double add(double a, double b) {
return a + b;
2. Runtime Polymorphism (Dynamic Binding):
- Achieved using method overriding.
- Decision made at runtime.
Example:
class Animal {
void sound() {
[Link]("Animal makes sound");
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
class Main {
public static void main(String[] args) {
Animal obj = new Dog(); // Runtime polymorphism
[Link](); // Output: Dog barks
Features / Objectives / Characteristics of Polymorphism:
- Code Reusability - Write generic and reusable code.
- Flexibility - Same method behaves differently depending on object.
- Extensibility - Easy to extend the application.
- Maintainability - Makes code cleaner and easier to manage.
- Efficiency - Reduces code complexity and improves execution efficiency.
Significance / Importance of Polymorphism:
- Encourages object-oriented design principles.
- Supports loose coupling and interface-based programming.
- Enhances code clarity and modularity.
- Makes applications easier to extend, debug, and maintain.
Principles of Polymorphism:
- Encapsulation and Inheritance are foundational.
- Utilizes dynamic method dispatch.
- Supports interface implementation and abstract classes.
- Based on the "one name, many forms" concept.