Monday, February 1, 2021

Polymorphism

Q. What is  OOP?

Ans: OOP(Object oriented programming) represent a programming methodology based on objects instead of just procedure and functions.

Q. What is polymorphism?

Ans: Polymorphism is the ability of an object to behave differently in different states.

  The most common use of polymorphism in oop occurs when an object of parent class hold the reference of child class object. 

Q. What is method Overloading and Overriding?

Ans: Overloading:-If a class has multiple method having same name but different in parameters list, it is known as method Overloading.

Note:There are two ways to overload the method.

          1. By changing number of argument

          2. By changing the data type.

Remember: Overloading is not possible by changing return type of method. it will cause compilation error.

Overriding: If child class have same method as declared in the parent class. it is known as method overriding in java.

Q. can you write return statement in finally block?

Ans: Yes, but you will get warning message at compile time.

Q. What are the difference between static polymorphism and dynamic polymorphism?

Ans: Static polymorphism is achieved by method overloading. Static polymorphism is a process in which a call to a overloaded method is resolved at compile time whereas Dynamic polymorphism is a process in which a call to an overriden method is resolved at runtime.

Overloading vs Overriding in Java

-----------------------------------------------------------------------------------
OverloadingOverriding 
1. More than one method with same name, different prototype in name scope is called method overloading1. More than one method with same name, same prototype in different scope is called method overriding
2. In case of method overloading, parameter must be different.2. In case of method overriding, parameter must be same.
3. Method overloading is performed within class3. Method overriding occurs in two classes.
4. In case of method overloading, return type can be same or different.4. In case of method overriding, return type must be same.
Q. What is Upcasting and Downcasting in java?
Ans: Upcasting: When Parent object holds the reference on a child then it is called upcasting. upcasting is  done automatically.
         Downcasting: vice versa but downcasting must be manually done by the programmer

Q. What is the senario of inheritance?
Ans: It is a parent child relationship when parent class need to provide some default behaviour and child class need to provide some specilized behavior.

No comments:

Post a Comment

String coding questioon

  How do you reverse a given string in place? ( solution ) How do you print duplicate characters from a string? ( solution ) How do you chec...