What is polymorphism? What is difference between static binding and dynamic binding? Explain with the help of an example.
Polymorphism in the context of Object-Oriented Programming (OOP) refers to the ability of a single entity, such as a method or class, to take on multiple forms. It allows objects to be treated as instances of their parent class, enabling code to be more flexible, reusable, and extensible. Polymorphism is one of the four fundamental principles of OOP, along with encapsulation, inheritance, and abstraction.
There are two types of polymorphism in Java: compile-time (or static) polymorphism and runtime (or dynamic) polymorphism.
1. Static Binding (Compile-Time Polymorphism):
Static binding occurs at compile time and is also known as early binding. In this type of polymorphism, the method that needs to be executed is determined during the compile-time based on the method signature. It is associated with method overloading, where multiple methods with the same name exist in the same class, but with different parameter lists.
```java
public class StaticPolymorphismExample {
public int add(int a, int b) {
return a + b;
}
_____ ________ ________ _______ _______ _____ _______ _____ _______ ______ _____ _________.
______ __________ __________ ____ _________ ________ _____ ______ ___.
__________ ________ __________ _______ _____ ____ ________ ____.
_____ ___ ___ _______ ________ ______ ______ _______ ____ ____ ____.
_________ _____ ________ ______ ___ _________ ___ _________ ____ ________.
______ ______ ________ ___ _________ ____ __________ _____ ______ ________ _____.
__________ __________ __________ __________ ________ _________.
____ _____ _________ _____ ___.
____ ______ ___ _______ ____ ____ ________.
__________ _________ _________ _______ _________ ____ ______ ______ _____ __________.
___ _______ _____ ______ __________ _____ ______ _________ ____.
_____ ______ _________ _________ _____.
______ _______ ______ _________ ___ _________ ________ ____ __________ __________.
____ _________ ________ ____ ___ ______ ________ __________.
___ ______ _________ __________ ________ ____ __________.
_______ ___ __________ ______ ___ _____ __________ ______ ____ ___.
_____ ______ ________ ___ ______ ________ ___ __________ ____ ___ _______.
__________ _____ __________ _________ ____ ________ ________ ______ _____ ____ ______.
___ ____ ________ ___ _______ __________ ______ ______ ______ ________ __________ __________.
_______ _______ __________ __________ _______ ________ _______ ________ _________ _____.
_________ __________ ______ ______ ________ ___ ______ __________ ______ _____ __________.
_______ __________ _____ ____ _____ ________ ______.
____ ____ _________ ______ _____.
___ ________ _______ ________ ___ ______ _____ ___ ________ ______ _______ __________.
_____ __________ _____ __________ ______ _____.
___ _________ ______ _______ ____ _________ ________ _________ _________ _________.
__________ ___ ___ _____ ____ ___.
______ ____ _____ __________ ____ ___.
___ _________ _________ _____ _________ _______ ____ _________ ___.
______ ______ ___ _________ _________ _______.
_____ ______ _________ ____ ______ _____ ____.
________ _________ __________ _________ _________.
__________ _______ ___ _______ ____.
_____ ________ _______ ________ ___ ______ ______ __________ _______ _______.
_____ ________ _______ _________ _________ _________ ______ __________ ____.
____ _______ __________ ____ _______.
_______ _________ _____ ______ ____ ___ _______ _____ _____.
__________ _______ ______ ______ __________ ___ ______ _____.
____ ________ _______ ________ ___ ________ __________ ______ _______.
______ ______ ___ _______ ______ _______ __________ ________ _____ _______ _______ ______.
__________ _____ _____ ____ ____ _________ ________ _______ __________ ________.
_________ _______ ________ _______ ________ __________ ____ _________ ________ _______.
_______ ___ ___ ________ ____ _________ __________ _____.
______ _________ ______ _____ _________ _____ __________ ___.
_________ ___ ____ _______ ________.
_______ __________ ______ _____ ______ ______.
_________ _____ ____ _______ __________.
___ ___ ________ ______ ___ ____.
______ ___ _____ ____ __________ __________ ____.
_____ ___ ____ ______ ____ ____ ________ _____ ______.
______ __________.
- Solved Assignments
- Study Notes
- Guess Papers
- Question Papers
✓ Unlock up to 60% of the answer
✓ No payment required