Polymorphism
- One thing can be behave in different manner. (One function can be used for a different ways/implementation)
- There are two types of polymorphism
- Compile time polymorphism
- The method call resolve at compilation time is called as compile time polymorphism.
- To Achieve this polymorphism can use Overloading.
- Runtime polymorphism
- The method call is resolve at execution time is called as runtime polymorphism.
- To Achieve this polymorphism can use Overriding.
- Compile time polymorphism
Overloading
- Same method name can be used for a different implementation in a same class or in sub class.
- Method name must be same with different input parameter list.
- Return data type may or may not be same.
- Access Modifier may or may not be same.
- Input parameter list must be different in following perspective.
- Can change the type of parameter.
- Can change the number of parameter.
- Can change the sequence of parameter.
- Overloaded methods gets invoked automatically on the bases of input values provided by user.
- Can overload a static method.
- Can overload the final methods.
- Can throw new and broader checked Exception.
Overriding
- Same Method name with same parameter list into a sub class.
- Method name must be same and Input parameter must be same.
- Overriding must done in sub class.
- Return data type must be same if it is void or primitive or it can be sub type.
- Access modifier must be same or less restrictive.
- Cannot override final method.
- Cannot override static method.
- Cannot throw new and broader checked Exception.
Achieve Runtime Polymorphism
- There must be a inheritance between class and sub class must override method.
- Create a Polymorphic Object. Polymorphic Object is a reference of Super class and Object of Sub class (Each super class reference can hold the object of all its sub classes).
- Call a overridden method by using polymorphic object.
Join Telegram : Click Here
All Full Stack Java Study Material
Job’s For Fresher