Java Interview Questions 6 of 9 - Polymorphism Flashcards
What is polymorphism and what are the types of it?
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.
What is compile time polymorphism?
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.
What is Runtime Polymorphism?
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.
Is it possible to achieve Runtime Polymorphism by data members in Java?
No. We need to create Runtime Polymorphism by implementing methods at two levels of inheritance in Java.
Explain the difference between static and dynamic binding?
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.
What is method overriding?
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.
What is method overloading?
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.
Difference between method overloading and overriding?
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.
- Can overload static, final or private method in Java Method Overriding - Can not override static, final and private method in Java
- Overloaded method in Java is bonded by static binding
Method Overriding - Overridden methods are subject to dynamic binding.
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.
Can we overload main() method?
Yes, we can have many main() methods in a class by overloading the main method.