Polymorphism :
Polymorphism is the ability of an object to take on many forms.The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object (inheritance).
Polymorphism in java is a concept by which we can perform a single action by different ways. Polymorphism is derived from 2 greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means many forms.
There are two types of polymorphism in java: compile time polymorphism (overloading) and runtime polymorphism (overriding). We can perform polymorphism in java by method overloading and method overriding.
public class Animal{} public class Deer extends Animal {}
Overloading Method :
Overloading is determined at the compile time. It occurs when several methods have same names with:a. Different method signature and different number or type of parameters.
b.Same method signature but different number of parameters.
c.Same method signature and same number of parameters but of different type
Overriding Method :
Overriding occurs when a class method has the same name and signature as a method in parent class. When you override methods, JVM determines the proper methods to call at the program’s run time, not at the compile time.
Some Interview Question:
Some Interview Question:
1.What is polymorphism and what are the types of it?
Polymorphism in java is a concept by which we can perform a single action by different ways.
Method overloading(compile time polymorphism),method overriding(run time polymorphism)
2.What is method overriding?
Specific implementation of a method for child class.
3.What is method overloading?
If a class have multiple methods by same name but different parameters, it is known as Method Overloading
4.What is static and dynamic binding?
static binding type of object is determined at compile time whereas in dynamic binding type of object is determined at run time.
5.can we overload main() method?
Yes, we can have many main() methods in a class by overloading the main method.
No comments:
Post a Comment