One the most important oops concept is the polymorphism. polymorphism means the same name or interfaces with multiple forms. So what is the polymorphism in Java, what is the use of polymorphism in java? how to achieve this in Java and at the end we will discuss some interview questions and answers based on polymorphism in java.
What is the polymorphism in Java?
Polymorphism is one thing with many behaviors. for example we can say like we have a class Car and a method price() so there will be a different price for every car. so price() method will be common but for every different car, it will return different price.
class Main { public static void main(String[] args) { Nexon nexon = new Nexon(); nexon.price(); EcoSport ecoSport = new EcoSport(); ecoSport.price(); Figo figo = new Figo(); figo.price(); } } class car { public void price() { System.out.println("Depends on car type"); } } class Nexon extends car { public void price() { System.out.println("Rs. 5.97 - 9.57 lakh"); } } class EcoSport extends car { public void price() { System.out.println("Rs. 7.31 - 10.92 lakh"); } } class Figo extends car { public void price() { System.out.println("Rs. 4.80 - 7.80 lakh"); } }
There are two types of polymorphism in Java.
- Runtime polymorphism.
- Compile time polymorphism.
Before this, you should know about method overloading and method overriding in Java.
Runtime polymorphism in Java.
Runtime polymorphism is a process in which a call to an overridden method is resolved at runtime rather than compile-time. In above car example, you can see there is parent class “Car” and price() is the method which is changing according to the object.
Note: We can’t be achieved Runtime polymorphism by data members.
Another example of runtime polymorphism is the bank.
class Main { public static void main(String args[]) { Bank b; b = new SBI(); System.out.println("Interest with SBI: " + b.getRateOfInterest()); b = new ICICI(); System.out.println("Rate of Interest with ICICI : " + b.getRateOfInterest()); b = new AXIS(); System.out.println("Rate of Interest with AXIS: " + b.getRateOfInterest()); } } class Bank { float getRateOfInterest() { return 0; } } class SBI extends Bank { float getRateOfInterest() { return 7.4f; } } class ICICI extends Bank { float getRateOfInterest() { return 5.3f; } } class AXIS extends Bank { float getRateOfInterest() { return 7.7f; } }
Compile time polymorphism in java.
Compile time polymorphism or method overloading. The polymorphism is performed at compile time is known as compile time polymorphism. Method overloading is an example of compile time polymorphism. It is also called static polymorphism.
Method Overloading: Multiple methods in the same class with the same name is called method overloading. definitely, there is some difference between the method is the return type of method or parameter of the method will be the difference.
In this example, Math class has sum() method 3 times. and every time method will return different output.
class Main { public static void main(String args[]) { Math obj = new Math(); obj.sum(); int sum = obj.sum(10, 20); int sum2 = obj.sum(10, 20, 30); System.out.println(sum); System.out.println(sum2); } } class Math { public int sum(int a, int b) { return a + b; } public int sum(int a, int b, int c) { return a + b + c; } public void sum() { System.out.println("0 sum"); } }
Some Important point you should remember about polymorphism in Java.
- Method Overriding or Dynamic Binding or Runtime Polymorphism is similar to each other in Java.
- Abstract methods must be overridden in Java.
- We can not override final methods in Java.
- We can not override static methods and constructors in Java.
- We can’t be achieved Runtime polymorphism by data members.
- We can overload constructor in Java.
Polymorphism in java with interview questions and answers
What is Polymorphism?
Define a method in multiple forms is called Polymorphism.
What is the method Overloading and its rule?
multiple methods with the same name but different method signature and different number or type of parameters.
There should we method return type or parameter will not be same of two methods.
What is method overriding?
Same method in the subclass and a parent class is known as method overriding in Java. It is determined at the run time.
Difference between method overloading and method overriding?
Method overloading is a compile-time polymorphism and method overriding is run time polymorphism.