The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form.
If we are taking a real time example, then a Person can be A Husband, A Son, An employee at time.
In JAVA there are mainly two types of polymorphism
- Compile time polymorphism
- Run time polymorphism
Compile time polymorphism
Compile time polymorphism also called as static polymorphism. This type of polymorphism achieved by method overloading.
Method overloading
Method overloading is a feature in which we can create more then one methods with the same name.
But the question is, why we need to make more then one methods with the same name?
For understanding this, we are taking an example of method from String class i.e. valueOf().
We can see that String.valueOf() method can take multiple type of arguments like Integer, Float, Double, Boolean, Long etc. We just need to pass the value into valueOf method, and it returns the String value.
Here we only need to know the name of one method for all type of arguments. That is the reason for method overloading, for making a single purpose methods or constructors with the different type or number of arguments.
Here I will provide you some points for method overloading
1. If methods have same name, have same number of arguments but different type of arguments, then it will be method overloading.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public void methodName(String arg){ } public void methodName(Integer arg){ } public void methodName(Float arg){ } public void methodName(String arg){ } public void methodName(Integer arg){ } |
2. If methods have same name, have same number and types of arguments but different position, then it will be method overloading.
1 2 3 4 5 6 7 | public void methodName(String arg1, Integer arg2){ } public void methodName(Integer arg1, String arg2){ } |
3. If methods have same name but different number of arguments, then it will be method overloading.
1 2 3 4 5 6 7 | public void methodName(String arg1){ } public void methodName(Integer arg1, String arg2){ } |
4. If methods have same name and same signature like same number of arguments and same type, same position, but different return type, then it will be not method method overloading.
1 2 3 4 5 6 7 | public Integer methodName(String arg1){ } public String methodName(String arg1){ } |
Basically return type of methods playing no role into method overloading. So please don't be confuse with return type.
Run time polymorphism
It is a process In which calling method is decided on runtime. It also known as Dynamic polymorphism.
It is achieved by method overriding.
Method overriding
In method overriding we define a method with the same name and same signature of Parent class.
It means, override method is a method, defined into child class, with the
- Same name.
- Same arguments (Same type and same order)
- Same return type
of parent class.
Into method overriding, return type also should be same. If you will try to change the return type, it will be compile time error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class ParentClass{ public String methodName(String name){ return "Parent class says hello to "+name; } } public class ChildClass extends ParentClass{ @Override public String methodName(String name){ return "Child class says hello to "+name; } } |
No comments:
Post a Comment