Sie sind auf Seite 1von 4

Question related to Polymorphism

1. What is Polymorphism and ways to supports in java?

Ans:
Polymorphism is the concept of “one interface and many implementations”.
Polymorphism support by java by two ways:
i) compile time or static polymorphism
ii) Runtime or Dynamic polymorphism.

2. What is Static or Compile-time Polymorphism?

Ans:
A polymorphism which achieved at the time of compilation is known as compile
time or static polymorphism.

3. What Runtime or Dynamic polymorphism?

Ans:
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?

Ans:
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?

Ans:
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?

Ans:
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?

Ans:
Multiple methods having same name, same return type and same argument list is
the concept of overriding.

8. Restriction in case of Overriding?

Ans:
The following are the restriction for overriding.
i) Method only can be override but constructor cannot be.
ii) Static method cannot override.
iii) Final method cannot override.

9. What these keywords refer?

Ans:
„this‟ keyword refer to the current class object.

10. What super keyword refers and states the use of super keyword?

Ans:
Super keyword always refers to the super class object. It can be used in 3 diff.
places in java
i) It is used to call from one to another constructor which present in two
different classes.
ii) When the data member of child class and parent class are same super
use to create a difference between them.
iii) 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?

Ans:
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?

Ans:
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?

Ans:
Overloading can be possible by two ways
They are
i) Method overloading
ii) Constructor overloading.

14. Overriding only possible in child class, prove it?

Ans:
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?


Ans:
this keyword.

16. Which keyword is used to create diff. b/w super class and child class if method is
same in both classes?

Ans:
Super keyword.

17. Which keyword use to call current class method?

Ans:
this keyword.

18. Which keyword is used to resolve conflicts between method parameter and
instance field/methods of invoked class?

Ans:
this keyword.

19. State the uses of „this‟ keyword all over in java?

Ans:
i) this keyword use to differentiate between local and instance variable if
both are same.
ii) this keyword use to call the current class object without creating any
object.
iii) this keyword use to call the current object hash code.
iv) this keyword use to call from one to another constructor without allocating
any memory.
v) this keyword can be post as an argument to method.
vi) this keyword can be post as an argument to constructor.

20. How overriding support Dynamic method dispatching?

Ans:
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?


Ans:
(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).

22. What are the differences between method overloading and method overriding?

Overloaded Method Overridden Method

Arguments Must change Must not change

Return Can change Can‟t change except for


type 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 Object type determines


which overloaded version is which method is selected.
selected. Happens at compile Happens at runtime.
time.

Das könnte Ihnen auch gefallen