InterviewSolution
Saved Bookmarks
| 1. |
What is the use of polymorphism in Java? |
|
Answer» A reason why polymorphism is needed in JAVA is that the concept is extensively used in implementing INHERITANCE. It plays a crucial ROLE in allowing the object having a different internal structure to share the same external interface. Example: class Vehicle{ void run(){System.out.println("running");} } class Bike extends Vehicle{ void run(){System.out.println("running safely with 60km");} public static void main(String ARGS[]){ Vehicle b = NEW Bike(); b.run(); } }In the above example, “class Bike extends Vehicle” means that the class Bike is inherited within the class Vehicle. With this, new internal structures can be created within the same external class. |
|