What is polymorphism Java interview question – Nicely answered

Answer includes, what is polymorphism java and how polymorphism achieved in java and a format how to answer this question in a technical interview to make impressive answer.

NOTE: On asking what is polymorphism, many people just give one line answer that polymorphism is the ability of an object to take on many forms. That’s it and leave the interviewer to ask next polymorphism questions.

Suggestion is that you answer entire concept of polymorphism until an interviewer ask to stop. This will give him complete understating of your skill.

Here is the answer format that we should consider to answer this question. In fact, this answer was appreciated by one of the highly experienced interviewer.

  • Polymorphism definition
  • How polymorphism provides simplicity? Give example
  • Polymorphism types – compile time and run time
  • Compile time polymorphism example – Constructor overloading and method overloading
  • Run time polymorphism – Method overriding example

Answer:

Polymorphism is the simplicity principle of java oops concept. Polymorphism states that a method can have many forms. In other words, a class can have many methods with same name called method overloading and a class and its derived class can have methods with same name called method overriding.

Let’s take an example that states how polymorphism provides simplicity. Take the below class Paint that has methods returning different colors where polymorphism – method overloading is not applied, for an example

 

 

Above class is the working class, but, the problem is that we need to remember all methods and not readable or not in specific format and difficult to find them.

 

Polymorphism overloading methods is applied here in below class. You decide yourself which one provides simplicity. We need to find only method and pass different parameters.

 

Java what is Polymorphism types

Two types of polymorphism are available in java object oriented programming i.e. overloading and overriding polymorphism.

  • Polymorphism overloading – method overloading and constructor overloading (same methods with different arguments within same class).
  • Polymorphism overriding – method overriding (same method in class and its derived class)

Constructor overloading and method overloading are also known as compile time polymorphism as they get resolved at compile time itself.

Java constructor overloading example

Below class has multiple Employee constructors overloaded with different signatures.

 

Java method overloading example

In below java program, print() method is overloaded with different parameters that is with int, String and int and String

 

 

Method overriding is run time polymorphism. The object’s method gets resolved and invoked during execution of the program.

Java method overriding example

Method overriding happens with base and child class in inheritance relationship. Below base class Songs has method play() and Sony class overrides and implemented its own play method and will not use play method from base class. If Sony class don’t override it then base class play () method will be called.

 

Read More