What is polymorphism in Java ? | Interview Question – Nicely answered

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

NOTE: On asking what is polymorphism in java technical interview questions, many people just give one line answer that polymorphism in java 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 with examples 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 USA interviewer in one of the  telephonic interview.

  • 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:

What is Polymorphism in Java?

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 in java oops provides simplicity.

Take the below class Paint that has methods returning different colors where polymorphism method overloading is not applied, for an example,

class Paint{
	
	public void getColourBlack() { }
	public void getColourRed() { }
	public void getColourRedBlue() {}	
	public void getcolourRedBlackBlue() {}
//more methods
	
}

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.

class Paint{
	
//all methods have same name accepting different
//color code
 public void getColor(int x) {}
 public void getColor(int x,int y) {}
 public void getColor(int x, int y, int z) {}
}

What is Polymorphism types in Java

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.

//Constructor overloading
class Employee{
	public Employee(int id){}
	public Employee(String name){}
	public Employee(int id, String name){}
	
}

Read in detail about java constructor overloading with complete example.

Java method overloading example

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

// Java method overloading
class Print {

	public void print(int id) {	}

	public void print(String name) {};

	public void print(int id, String name) {}
}

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.

// base class
class Songs {

	public void play() {
		System.out.println("play implementation by default songs");
	}
}

// derived class
class Sony {
	// overriden play method
	public void play() {
		System.out.println("play implementation by Sony");
	}
}

You can read more on method overriding in java with real time examples for learning more, but, i had not included more examples other than above polymorphism explained.

Related Posts