DAY 13 Polymorphism:
What is Polymorphism?
In simple terms, polymorphism means "many forms," enabling a single method to work in different ways based on the object that it
is acting upon.
Types of Polymorphism
1.Compile-Time Polymorphism (Method Overloading):
- Resolved during compile time.
- Achieved through method overloading.
2. Run-Time Polymorphism (Method Overriding):
- Resolved during runtime.
- Achieved through method overriding.
Now what is Method overloading and Method overriding:
1. Compile-Time\Early Binding\Static Polymorphism (Method Overloading)
In method overloading, as name indicates in happens during compile time multiple methods have the same name but differ in:
- The number of parameters.
- The type of parameters.
- The order of parameters.
Example:
2. Run-Time Polymorphism (Method Overriding)
In method overriding, a subclass provides a specific implementation for a method that is already defined in its parent class.
- It Involves inheritance.
- The overridden method in the child class must have the same name, return type, and parameters as the method in the parent class.
- Uses the @Override annotation for clarity and to avoid errors.
Let's see few common differences between Method Overloading and Overriding with respect to Arg type, name, Return type, Access modifier, Throwing exceptions, Binding:
Let's see Encapsulation tomorrow. Happy Learning :)