Interview Questions on Java - Polymorphism
1. What is Polymorphism?
The ability to take more than one form is called polymorphism. There are two types polymorphism.
1. Compile time Polymorphism( or Static polymorphism) : It will be decided at the time of Compile time. ex: Method OverLoading.
2. Runtime Polymorphism( or Dynamic polymorphism): It will be decided at the time of Running time. ex: Method Overriding.
2. What is Method Overloading?
If multiple methods have same name but different signature then we will call it Method Overloading.
3. Can we use the same Method name twice by differing return type?
No. Because we won't use return type while calling the method.
4. What is Method Overriding?
The process of replacing the parent class method with child class method is called method overriding.
5. What is the main difference between Method Overloading and Method Overriding?
In Method Overloading Signature must be different. In Method Overriding Signature must be same.
6. What is co-variant return type?
While happening method overriding, the return type of child class can be subclass of parent class return type. this is nothing but c0-varient return type.
7. If the access specifier of parent class method is protected then what could be the access specifier of child class method?
protected or public. Because access specifier should be equal or wider than its.
8. If Parent class method throws some exception then what type of exception could be thrown at child class?
It should be same type or subclass to that exception.
9. Does static methods inherits to the child class?
10. How to call Parent class methods in child class?
By using "super" key word.
11. How to achieve perfect run time polymorphism?
To achieve perfect polymorphism two things shpuld be happen
1. auto upcasting
2. method overriding