Selenium/Java-OOPs-Polymorphism
In last post, we learnt about Inheritance concept in OOPs. In this post, we will go through the one last important concept of OOPs that is nothing but Polymorphism. As the name suggests, Polymorphism means multiple forms. Here, it is the ability of method to perform different actions based upon which object is being called or referred to.
There are mainly two types of Polymorphism namely static and dynamic. We will see both the types along with example.
![]() |
[Source: www.youtube.com] |
- Static Polymorphism: In this type of Polymorphism, we can have multiple methods with same name. Compiler will distinguish these methods with their method signatures.. !
It is also called as compile time polymorphism as at the time of compilation, it will be decided that which method will be called. It can be useful when one class has many methods with same name.
It is defined as method overloading.
Example:
Suppose, there is one bank who offers different rate of interest on savings account to normal and privileged customers. For privileged customers they offer extra special interest.
class Bank{
// deposit method for normal customer
void deposit(int principal, int interest){
amount=principal+(principal*interest)/100;
System.out.println("The total amount will be "+amount);
}
// deposit method for privileged customer
void deposit(int principal, int interest, int special interest){
amount=principal+(principal*interest)/100+(principal* special interest)/100;
System.out.println("The total amount will be "+amount);
}
public static void main(String[] args){
Bank x= new Bank();
x.deposit(1000,4); // compiler will distinguish methods based upon their signature
x.deposit(1000,4,1);
}
}
- Dynamic Polymorphism: In this type of Polymorphism, which method to be called is decided during runtime. Hence, it is also called as runtime polymorphism or dynamic method dispatch.
It is most widely used form of polymorphism. Here, methods with same names can be present in parent as well as child class and parent class can refer to child class objects.It is called as up casting.
It is defined as method overriding.
Example:
Suppose one bank offers loans with different interest rates.
class Loan{
// method in parent class
void interestRate(){
System.out.println("The interest rate will be 5%");
}
}
// child class
class homeLoan extends Loan{
// method with same name as parent class in child class
void interestRate(){
System.out.println("The interest rate will be 7%");
}
}
// child class
class carLoan extends Loan{
//method with same name as parent class in child class
void interestRate(){
System.out.println("The interest rate will be 9%");
}
public static void main(String[] args){
// parent class referring to child class object resulting in method overriding
Loan l= new homeLoan();
l.interestRate();
Loan k= new carLoan();
k.interestRate();
}
}
This is all about Polymorphism in Java. Run the code and observe results !!
Cheers !
Short and sweet..
ReplyDeleteYes, that's the magic of OOPs. It makes things simple :)
Delete