Java Examples

Core Java interview questions on Polymorphism

1.What is polymorphism and what are the types of it?
Single task can be done in different way.
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.Difference between method overloading and overriding?

Method overloadingMethod overriding
In case of method overloading, signature of method changesWhile in case of method overriding it remain same.
Can overload method in one class Can only be done on subclass.
Can overload static, final or private method in JavaCan not override static, final and private method in Java
Overloaded method in Java is bonded by static bindingOverridden methods are subject to dynamic binding.

5.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.

6.Why method overloading is not possible by changing the return type in java?

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

Example:

public class overldmain
{
	public static void main(String[] args)
	{
		System.out.println("main(String[] args)");
	}
 
	public static void main(String args1)
	{
		System.out.println("main(String arg1)");
	}
 
	public static void main(String arg1, String arg2)
	{
		System.out.println("main(String arg1, String arg2)");
	}
}

8.What is run time polymorphism and compile time polymorphism?
compile time polymorphism:
it is nothing but 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.
Runtime polymorphism:
Runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time.
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.