Introduction Polymorphism in JAVA is an important topic in the OOPS concept. In this post, we will discuss polymorphism in details. Polymorphism is a concept of performing a single action in different ways. Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. There are two types of polymorphism in Java: compile-time polymorphism(method overloading) and runtime polymorphism(method overriding).
Overloading - Static Polymorphism Two methods are said to be overloaded, if they have the same name but different parameters(arguments). Method overloading increases readability of the program. There are two ways to overload a method in java.
By changing number of arguments - Suppose we create a function to add numbers. We can overload it by changing the number of arguments in the following manner.
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
By changing the data type - We can also overload a method by changing the data type of the arguments and the return type of the method. This can be done as follows.
static int add(int a, int b){return a+b;}
static double add(double a, double b){return a+b;}
Note: We can't overload a method just by changing the return type of the methods. This will result in ambiguity while calling the method from a different class. This can be explained as follows.
class Sum{
static int add(int a,int b){return a+b;}
static double add(int a,int b){return a+b;}
}
class Test{
public static void main(String[] args){
System.out.println(Sum.add(11,11));
}
}
In the above example while invoking the add method of the Sum class, JVM is not sure which method to call as the data type of arguments are same for both the overloaded methods and this results in ambiguity.
Overriding - Dynamic Polymorphism A child class inherits the variables and methods of the parent class. Sometimes it may happen that the child class wants to implement the method from the parent class in its own way. This is when the parent method is overridden in the child class. Following are the rules for method overriding.
The method must have the same name as in the parent class.
The method must have the same parameter as in the parent class.
Let's take an example of the rate of interest in different banks. We have a parent class Bank.java and other classes extends the Bank class and provides their own implementation for the rate of interest.
class Bank{
int getInterest(){return 9;}
}
class SBI extends Bank{
int getInterest(){return 8;}
}
class ICICI extends Bank{
int getInterest(){return 7;}
}
class Test2{
public static void main(String args[]){
SBI s=new SBI();
ICICI i=new ICICI();
System.out.println("SBI Interest: "+s.getInterest());
System.out.println("ICICI Interest: "+i.getInterest());
}
}
Overloading v/s Overriding In this section we will discuss about the differences between the two types of polymorphism.
Argument types : Must be different in overloading(atleast order of the arguments), must be same in overriding
Return type : No restriction in overloading. In case of overriding the return type must be same but from JAVA v1.5 onwards, we can use co-variant return types(It means that the child overriding method can have a return type that is a child of the parent method's return type)
Private, static, final methods : Can be overloaded. can't be overridden
Access modifiers : No restrictions in overloading. For overriding, we can't decrease the scope of access modifiers in the child class, we can only increase it. For example: If the parent has the method as protected, we can either use protected or public in the child class.
Exception handling : No restriction in overloading. In case overriding, if the overriding method in child class throws a checked exception, the parent overridden method must throw the same exception or it parent exception. For example, if the child method throws I/O exception, the parent method should throw either I/O Exception or its parent
Summary In this post, we have learnt about polymorphism in JAVA and discussed the two types of polymorphism. Also discussed the difference between the two.
Comments