Lec17 Polymorphism
Lec17 Polymorphism
Example- Light button, we are using that button to on or off the lights.
1. Method overloading-
2. Method overriding-
1. Method overloading-
It is the same method name with different argument called as Method
overloading. There is no need of super and sub class relationship. It is called as early
binding, compile time polymorphism or static binding.
Rules-
Example-1
package com.tests;
void add(double a) {
System.out.println(a);
}
Output is
10.5
22.0
6
30
Why?
Suppose we got the business requirement from the client in last year
Class Employee {
//End User 1
addStudent (“ram”,”pawar”,”Pune”);
//End User 2
addStudent (“ram”,”deshmukh”,”Mumbai”);
After that I got the new requirement from the client in current year, to update the
pan card details.
First way modifying into existing method is not good approach, it will increase the
unit testing of it. If we are make the changes into existing method, then how user
calls the method I mean they need to add one more extra field, in future again, you
got requirement to add one more field so every time user need to change at their
side, this is not the good thing.
Second way, create the same method in that class and add the new field into it. If
client second want pan card details so he can call that method otherwise calls the
first method if pan card is not required.
Example- 2
package com.poly;
public class A{
void test(Object object) {
System.out.println("test- Object");
}
test- Object
test- String
test- Object
test- String
Because it is decided at compile time which one method should get called that’s why it is
called as compile time polymorphism.
2. Method overriding-
It is the same method name with same argument called as method overriding.
There is need of super and sub relationship. It is called as late binding, run time
polymorphism or dynamic binding.etc.
If subclass (child class) has the same method as declared in the parent class, it is known
as method overriding in Java.
Rules-
Method name must be same.
Return type must be same or different.
Access specifier is anything.
Parameters must be same.
Note- we can extend the method scope in overriding but not reduce the visibility of it.
Why?
Maintainability
Readability of code.
Example-
package com.override.demo;
public class A {
void m1() {
System.out.println("class - A- m1 () method");
}
package com.override.demo;
@Override
void m1() {
System.out.println("class - B- m1 () method");
}
void m7() {
System.out.println("class- B- m7() method");
}
}
package com.override.demo;
B b= new B();
b.m1();
b.m7();
}
}
Output-
class - B- m1 () method
class- B- m7() method
Program Explaination-
In the above program, B is implementing the method m1 () with the same signature
If you want to add new features to existing class, then you should not disturb the
existing class. You should always write the subclass of that class that is the best
practice.
Subclass method's access modifier must be the same or higher than the superclass method
access modifier
//logic here
}
//logic here
//logic here
}
}
Live Example-2
Class Banking{
void getBanking(Credit card){
//logic here
}
void getBanking(netbanking{
//logic here
//logic here
void getBanking(UPI){
//logic here
}
}
Class SBI {
void getSimpleIntereset(int simpleRate){
//logic here
}
}
//logic here
}
}
Class HDFC extends Axis {
//logic here
}
}
Live Example-2
Class FirstTier {
void getSeatAvailability(int seat){
//logic here
}
}
}
}
Class ThirdTier extends SecondTier {
//logic here
}
}