Interview Questions on Polymorphism in Java

What is Polymorphism in Java?

Polymorphism is derived from two greek words, ‘Poly’ which means ‘many’ and ‘morph’ which means ‘form’. Therefore, we can say that Polymorphism is the ability of an object to exist in multiple forms.

How can Polymorphism be achieved in Java?


Through method overloading and method overriding.

Can we extend a String class?

String is a final class, therefore we cannot extend or inherit it.

What is compile-time and runtime polymorphism?

Compile-time polymorphism or Static polymorphism is what we better know as method overloading and Runtime polymorphism or Dynamic polymorphism is what we better know as method overriding.

When do we need to override hashcode() and equals() method?

The default implementation of equals() method tests whether the two concerned objects point to the same memory location in java virtual memory. However, String class overrides the equals() method of Object class, NOT to check the reference of the variables, but instead the character sequence in the value the concerned strings hold. If this is not what you want, and you can specify your own standard for deciding object equality.

Note that, if we override equals() method, you also need to override the hashcode() method. This is because when a.equals(b) method is true, a.hashcode() == b.hashcode() should also be true. This must be very carefully handled if we are using these objects(a, b types) of the concerned class as Keys in Map implementations or Values in Set implementations, to be able to uniquely identify an entry based on the Key(in Map implementations) or Value(in Set implementations). It is fine not to override the hashcode() method even after overriding the equals() method, but only in those cases where we are not using any of the Map implementations to store such objects. But we should always override the hashcode() method whenever there is a need to override the equals() method.

Can we override private methods in Java?

No, since the scope of private methods is only upto the respective class and overriding never occurs within a single class.

Can we override static methods in Java?

I found the best explanation here.

Can we override or overload main method?

We can overload the main() method, but only public static void main(String[] args) will be used when our class is launched by the JVM.

We cannot override the main method, since it is static and hence polymorphism cannot be achieved.

The overriding method might not throw an exception that is thrown by the overridden method. The vice-versa of the same is not at all possible. However, the exception thrown by the overriding method must always be lower in hierarchy than that of the overridden method in the parent class.

Sample Reference Snippet for Method Overloading & Overriding

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.