Polymorphism
- By using this concept we can able to perform the same action in different ways
- This is achieved by using method overloading and method overriding
Types of Polymorphism
- Runtime Polymorphism or Dynamic Polymorphism
- Compile-time Polymorphism or Static Polymorphism or early binding
1. Runtime Polymorphism or Dynamic Polymorphism
This is achieved using method is overridden and method overriding. Assume a class is having some method implementation and in your program, if you want to do your own implementation with the same method name, you need to override the implemented method.
Let us discuss in detail using the sample program
----------------------------------------------------------------------------------------------------------------
package oops;class Boss{//This is overridden methodpublic void project(){System.out.println("This project is from overridden method");}}public class Polymorphism extends Boss {//This is overriding methodpublic void project() {System.out.println("This method overrides project() in Boss class");}public static void main(String[] args) {//Creating object reference for Polymorphism classPolymorphism p = new Polymorphism();//Calling the methodp.project();}}
----------------------------------------------------------------------------------------------------------------
Output:
This method overrides project() in Boss class
Program Explanation
In the above program, we had a parent class as Boss with method Project().
Another class Polymorphism extends Boss class and had it's own implemented method with the same name as Project()
In this scenario, your own implementation made in the child class will get applied for your program and the parent class method will get overrides
If you want to use both method implementation in different places of your program you can create an object for the parent class and call the method you want to use.
Where can this concept be useful?
- Both overridden and overriding method should have the same number and arrangement of an argument
- Access modifiers have a certain limitation. The overriding method should be less restrictive than the overridden method. For example, if the method in the parent class(Overridden) is protected, Method in the child class(Overriding) should be public and the compiler will not accept either private or default. Below are a small comparison of modifier properties
- Private - Accessible within the class
- Default - Accessible within the class and package
- Protected - Accessible within the class, package, and subclass outside the package
- Public - Accessible within the class, package, subclass outside the package, and outside package
- The overridden method or overriding method should not be static, final, or private. Both overridden and overriding methods can be static
2. Compile time polymorphism or static polymorphism
In this concept, both methods having the same name but the arguments passed to those methods are different. There are certain scenarios that can be done using this concept and some we can't do with this concept. Let us see that in detail
Different datatype
In this scenario, the method in the parent class had arguments as int and String. And method in the child class had arguments with data type as int and int.
Sample Program
----------------------------------------------------------------------------------------------------------------
package oops;class Boss{//Argument in this method of data type int,intvoid project(int a, int b){System.out.println("Had both integer values " +a + ", "+b);}}public class Polymorphism extends Boss {//Argument in this method of data type int,Stringvoid project(int a, String b){System.out.println("Had one integer value = " +a + " and other string value = "+b);}public static void main(String[] args) {Polymorphism p = new Polymorphism();//Passing argument for datatype int,Stringp.project(5,"Poly");}}
----------------------------------------------------------------------------------------------------------------
Output:
Had one integer value = 5 and other string value = Poly
In the above example parent class had a method named project with argument data types as int,int. In the child class, we had the same method name with different argument data types like int,String. When we pass the respective data type to the method it will execute the method which matches the user passed argument.
If we pass below value in the above program,p.project(5,"Poly"); output will be "Had one integer value = 5 and other string value = Poly"If we pass below valuep.project(5,6); output will be "Had both integer values 5, 6"
Different number of argument
In this scenario, the method in the parent class had 2 arguments, and the method in the child class had 3 arguments. And both method names are the same
Sample program
----------------------------------------------------------------------------------------------------------------
package oops;class Boss{//Argument in this method of data type int,intvoid project(int a, int b){System.out.println("Had both integer values " +a + ", "+b);}}public class Polymorphism extends Boss {//Argument in this method of data type int,int, intvoid project(int a, int b, int c){System.out.println("Had one integer value = " +a + " and second int value = "+b + " and final int value as = "+c);}public static void main(String[] args) {Polymorphism p = new Polymorphism();//Passing argument for datatype int,int, intp.project(5,6,9);}}
----------------------------------------------------------------------------------------------------------------
Output:
Had one integer value = 5 and second int value = 6 and final int value as = 9
Program Explanation
In this example, we had two methods of the same name but a different number of arguments. The method in parent class had 2 arguments and both are of data type as int. The method in child class had 3 arguments and all are int data type. Based on the number of arguments passed on that method, the program will get executed.
Extra points
- If you use the same number of argument and of same data type, the method will get overridden as we mentioned in Runtime polymorphism
- We also can use the above mentioned 2 scenarios inside the same class.
Post your questions in the comment to get clarified and support us to improvise us.
Comments
Post a Comment