Java Interview Questions 6 of 9 - Polymorphism Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is polymorphism and what are the types of it?

A

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.

There are two types of polymorphism in java and these are given below.

i. Compile-Time polymorphism or Static polymorphism.
ii. Run-time polymorphism or Dynamic polymorphism.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is compile time polymorphism?

A

Compile time polymorphism is the method overloading in java. In simple terms we can say that a class can have more than one methods with same name but with different number of arguments or different types of arguments or both.

Whenever an object is bound with their functionality at compile time is known as compile-time or static polymorphism in java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Runtime Polymorphism?

A

Runtime Polymorphism or Dynamic Polymorphism is the polymorphism that exists at runtime. In case of method overriding it is not known which method will be called at runtime. Based on the type of object, JVM decides the exact method that should be called. In this process, an overridden method is called through the reference variable of a super class. The determination of the method to be called is based on the object being referred to by the reference variable.

So at compile time it is not known which method will be called at run time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Is it possible to achieve Runtime Polymorphism by data members in Java?

A

No. We need to create Runtime Polymorphism by implementing methods at two levels of inheritance in Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain the difference between static and dynamic binding?

A

In Static binding references are resolved at compile time. In Dynamic binding references are resolved at Run time.

Static binding is also known as compile - time polymorphism. Method overloading is the example of dynamic bindingIn static binding, The type of object is determined at compile-time.Static binding is also known as early 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is method overriding?

A

Specific implementation of a method for child class.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 is the example of compile-time polymorphism.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is method overloading?

A

If a class have multiple methods by same name but different parameters, it is known as Method Overloading. 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Difference between method overloading and overriding?

A

Method overloading vs. Method overriding
1. In case of method overloading, signature of method changes.
Method Overriding - it remain same.

2. Can overload method in one class
Method Overriding - Can only be done on subclass.
  1. Can overload static, final or private method in Java Method Overriding - Can not override static, final and private method in Java
  2. Overloaded method in Java is bonded by static binding
    Method Overriding - Overridden methods are subject to dynamic binding.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is static and dynamic binding?

A

static binding type of object is determined at compile time whereas in dynamic binding type of object is determined at run time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Can we overload main() method?

A

Yes, we can have many main() methods in a class by overloading the main method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly