Java Interview Questions - Polymorphism
1. What is Polymorphism and ways to supports in java?
Polymorphism is the concept of “one interface and many implementations”. Polymorphism support by java by two ways:
- compile time or static polymorphism
- Runtime or Dynamic polymorphism.
2. What is Static or Compile-time Polymorphism?
A polymorphism which achieved at the time of compilation is known as compile time or static polymorphism.
3. What Runtime or Dynamic polymorphism?
It is a process in which a call to an overridden method is resolved at runtime rather than compile time.
4. What are constructor overloading and its benefits?
Constructor overloading – When more than one constructor appear with in the single class by mismatching the argument known as Constructor overloading.
5. What are method overloading and its benefits?
Method overloading-When name of the method is same and appear more than one time with a single class but by taking different argument known as Method overloading.
6. What is method overriding?
When the child and parent class method name is same as well as the argument is known as method overriding.
7. What is overriding and how java supports it?
Multiple methods having same name, same return type and same argument list is the concept of overriding.
8. Restriction in case of Overriding?
The following are the restriction for overriding.
- Method only can be override but constructor cannot be.
- Static method cannot override.
- Final method cannot override.
9. What these keywords refer?
“this” keyword refer to the current class object.
10. What super keyword refers and states the use of super keyword?
Super keyword always refers to the super class object. It can be used in 3 different places in java.
- It is used to call from one to another constructor which present in two different classes.
- When the data member of child class and parent class are same super use to create a difference between them.
- If the method of super class and child class are same super use to create a difference between them.
11. What is dynamic method dispatching?
Dynamic method dispatching is a process in which a call to an overridden method is resolved at runtime rather than at compile time.
12. What is up casting and down casting?
Up Casting - When reference variable of parent class refers to the object of child class, it is known as up casting. Down Casting - When subclass type refers to the object of parent class, it is known as down casting.
13. How many ways overloading can be possible &What are they?
Overloading can be possible by two ways They are:
- Method overloading
- Constructor overloading.
14. Overriding only possible in child class, prove it?
At the time of overriding the method child class method should contains same access specifier which present in super class method.
15. Which keyword use to print current object hash code?
this keyword.
16. Which keyword is used to create diff. b/w super class and child class if method is same in both classes?
Super keyword.
17. Which keyword use to call current class method?
this keyword.
18. Which keyword is used to resolve conflicts between method parameter and instance field/methods of invoked class?
this keyword.
19. State the uses of „this‟ keyword all over in java?
- this keyword use to differentiate between local and instance variable if both are same.
- this keyword use to call the current class object without creating any object.
- this keyword use to call the current object hash code.
- this keyword use to call from one to another constructor without allocating any memory.
- this keyword can be post as an argument to method.
- this keyword can be post as an argument to constructor.
20. How overriding support Dynamic method dispatching?
An overridden method is called through the reference variable of a super class which resolved at runtime like overridden support Dynamic method dispatching.
21. How java implements polymorphism?
(Inheritance, Overloading and Overriding are used to achieve Polymorphism in java). Polymorphism manifests itself in Java in the form of multiple methods having the same name. In some cases, multiple methods have the same name, but different formal argument lists (overloaded methods).
In other cases, multiple methods have the same name, same return type, and same formal argument list (overridden methods).
- What are the differences between method overloading and method overriding?
Type | Overloaded Method | Overloaded Method |
---|---|---|
Arguments | Must change | Must not change |
Return type | Can change | Can‟t change except for covariant returns |
Exceptions | Can change | Can reduce or eliminate. Must not throw new or broader checked exceptions. |
Access | Can change | Must not make more restrictive (can be less restrictive) |
Invocation | Reference type determines which overloaded version is selected. Happens at compile time. | Object type determines which method is selected. Happens at runtime. |