Java Polymorphism Interview Questions
Here we will see some java interview questions on polymorphism which is mostly asked in any java interviews.
Polymorphism in java interview questions and answers for experienced and beginners both.
Polymorphism in java interview questions and answers for experienced and beginners both.
(1) What is polymorphism in java?
In java, Polymorphism means "one name many forms". In other words, you can say whenever an object producing different-different behavior in different-different circumstances is called polymorphism in java.
1) Related to person
2) Related to product
Let's take an example of an Air conditioner which produces hot air in winter and cold air in summer.
So there is one name but it producing different - different output according to the situation.
(2) Give real-life examples of polymorphism
Let's understand real-time example of polymorphism.
- Suppose you are in the classroom that time you will behave like a student.
- Suppose when you at home you behave like son or daughter.
- Suppose you are in the market at that time you behave like a customer.
2) Related to product
Let's take an example of an Air conditioner which produces hot air in winter and cold air in summer.
So there is one name but it producing different - different output according to the situation.
(3) How many types are of polymorphism in java?
There are two types of polymorphism in java and these are given below.
- Compile-Time polymorphism or Static polymorphism.
- Run-time polymorphism or Dynamic polymorphism.
(4) Can we achieve polymorphism through data member?
No, Polymorphism is always achieved via behavior of an object only properties of an object do not play any role in case of polymorphism.
(5) What are compile-time and run-time polymorphism?
Compile-Time Polymorphism
The best example of compile-time or static polymorphism is the method overloading. Whenever an object is bound with their functionality at compile time is known as compile-time or static polymorphism in java.
Run-Time Polymorphism
The best example of run-time or dynamic polymorphism is the method overriding. Whenever an object is bound with their functionality at run-time is know as run-time or dynamic polymorphism in java.
(6) What is method overloading?
Whenever we have more than one same name method but a different number of arguments or different data types in the same class is known as method overloading in java. Method overloading the example of compile-time polymorphism.
For example:
class Addition
{
void add(int a, int b)//with 2 arguments
{
System.out.println(a+b);
}
void add(int a, int b, int c)//change no of arguments i.e 3
{
System.out.println(a+b+c);
}
public static void main(String args[])
{
Addition a = new Addition();
a.add(10,20);
a.add(20,5,6);
}
}
Output: 30
31
Output: 30
31
(7) What is method overriding?
In java, whenever we have same name method in parent and child class with the same number of arguments and same data types is known as method overriding in java. Method overriding is the example of dynamic polymorphism.
For example:
class Parent
{
{
void show()
{
System.out.println("Parent");
}
}
class Child extends Parent
{
void show()
{
System.out.println("Child");
System.out.println("Child");
}
public static void main(String args[])
public static void main(String args[])
{
Parent p =new Child();
Parent p =new Child();
p.show();
}
}
Output: Child
(8) How to achieve static polymorphism?
Compile - time polymorphism is also known as static polymorphism. We can achieve static polymorphism through method overloading in java.
(9) How to achieve dynamic polymorphism?
Run-time polymorphism is also known as dynamic polymorphism. We can achieve dynamic polymorphism through method overriding.
(10) Difference between method overloading and method overriding?
Click on this link to see the differences between method overloading and method overriding in java.
(11) What is up-casting in java?
Whenever we put the reference id of a child class object into the parent class reference variable is known as upcasting in java.
For example:
Parent p = new Child();//upcasting declaration
(12) What is static binding?
Static binding is also known as compile - time polymorphism. Method overloading is the example of static binding.
In static binding, The type of object is determined at compile-time.
Static binding is also known as early binding.
(13) What is dynamic binding?
Dynamic binding is also known as run-time polymorphism. Method overriding is the example of dynamic binding.
In dynamic binding, The type of object is determined at run-time.
Dynamic binding is also known as late binding.
(14) Can we achieve method overloading by changing the return type?
No, We cannot achieve method overloading through return type in java.
(15) What is runtime polymorphism?
Runtime polymorphism is also known as method overriding and it is achieved at run-time.
Read More:
Java Method Overloading Interview Questions.
OOPS Interview Questions and Answers in Java.
Java Static Keyword Interview Questions.
Java Collection Interview Questions.
Java Constructor Interview Questions.
Read More:
Java Method Overloading Interview Questions.
OOPS Interview Questions and Answers in Java.
Java Static Keyword Interview Questions.
Java Collection Interview Questions.
Java Constructor Interview Questions.
Polymorphism is a powerful concept of oops(Object Oriented Programming System). Above all the polymorphism interview questions and answers are quite useful for java competitions.
Java polymorphism is most important topic for core java interview.
ReplyDeleteThank you so much for this great post.
Thanks for the simple explanation . You can learn more in details here Java Polymorphism Questions
DeleteGreat Article android based projects
DeleteJava Training in Chennai Project Center in Chennai Java Training in Chennai projects for cse The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training Project Centers in Chennai
Learned a lot of new things from your post!Good creation ,It's amazing blog
ReplyDeleteCore Java Online Training
Java Online Course
Java Online Training Bangalore
Nice post.useful for job seekers Java online course Bangalore
ReplyDeleteNice. Also check at linux training
ReplyDeletemulesoft training
I really enjoy the article.Thanks Again. Fantastic.
ReplyDeletebest online java course
advanced java course
nice post.
ReplyDeletebest online java course
advanced java course
This is an excellent post - thank you for sharing it! I always relish the opportunity to read such superb content filled with valuable information. The ideas presented here are truly best and exceptionally captivating, making the post thoroughly enjoyable. Please continue with your fantastic work.
ReplyDeleteUnveiling the Power of Java Streams API