This website includes Education Information like a programming language, job interview question, general knowledge.mathematics

Education log

PageNavi Results No.

Ads

Wednesday, June 9, 2021

what is polymorphism in java with example

 what is polymorphism in java with example


Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance. ... Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.



What is polymorphism and example?

The word polymorphism means having many forms. ... Real life example of polymorphism: A person at the same time can have different characteristics. Like a man at the same time is a father, a husband, an employee. So the same person posses different behavior in different situations. This is called polymorphism.



Runtime Polymorphism in Java

Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time.


In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.


Let's first understand the upcasting before Runtime Polymorphism.





polymorphism example in java : 

class Bank{  

float getRateOfInterest(){return 0;}  

}  


class SBI extends Bank{  

float getRateOfInterest(){return 8.4f;}  

}  


class ICICI extends Bank{  

float getRateOfInterest(){return 7.3f;}  

}  


class AXIS extends Bank{  

float getRateOfInterest(){return 9.7f;}  

}  


class TestPolymorphism{  

public static void main(String args[]){  

Bank b;  

b=new SBI();  

System.out.println("SBI Rate of Interest: "+b.getRateOfInterest());  


b=new ICICI();  

System.out.println("ICICI Rate of Interest: "+b.getRateOfInterest());  

b=new AXIS();  

System.out.println("AXIS Rate of Interest: "+b.getRateOfInterest());  


}  

}  



No comments:

Post a Comment