18 Polymorphism
18 Polymorphism
18 Polymorphism
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.
Having overloading concept in java reduces complexity of the
programming.
Example-1
public class Test {
Output is
10.5
22.0
6
30
Point to be remember
1) Method name must be same.
2) Parameter or argument must be different. (sequence of argument,
number of argument or datatype should be different)
3) Return type can be anything
4) Access specifier can be anything
5) Exception thrown can be anything
Why?
Suppose we got the business requirement from the client in last year
Class Employee {
Void addStudent (String firstname, string lastname, string city) { }
End user is calling the class as below
//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.
What options we have?
1. Modified into the existing method.
2. Create the new method with new parameter.
First way modifying into existing method is not good approach, it will
increase the unit testing of it. If we are making the changes into existing
method, then how existing user will call the method I mean they need to
add one more extra attribute, in future again, you got requirement to add
one more attribute 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
attribute 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
public class TestMain {
public void methodOne(int i) {
System.out.println("int-arg method");
}
Example- 3
package com.poly;
public class A{
2.Overriding
1. Whatever the Parent has by default available to the Child through inheritance,
if the Child is not satisfied with Parent class method implementation then Child
is allow to redefine that Parent class method in Child class in its own way this
process is called overriding.
2. The Parent class method which is overridden is called overridden method.
3. The Child class method which is overriding is called overriding method.
Point to be remember
public class A {
void m1() {
System.out.println("class - A- m1 () method");
}
@Override
void m1() {
System.out.println("class - B- m1 () method");
}
void m2() {
System.out.println("class- B- m2() method");
}
}
public class TestMain {
B b= new B();
b.m1();
b.m2();
}
}
Output-
class - B- m1 () method
class- B- m2() method
Until 1.4 version the return types must be same but from 1.5 version onwards
covariant return types are allowed.
According to this Child class method return type need not be same as Parent
class method return type its Child types also allowed.
class Parent {
public Object methodOne() {
return null;
}
}
class Parent {
public void methodOne() { }
}
class Child extends Parent {
protected void methodOne( ) { }
}
Output:
Compile time error
class RBI {
void getSimpleIntereset(float simpleRate) {
//logic here
}
}
//logic here
}
}
//logic here
}
}
Live Example-2
class Policy {
}
}
}
}