What is Polymorphism?
Polymorphism is the ability of an object to take on many forms.
The most common use of polymorphism occurs when a parent class reference is used to refer to a child class object.
Real life example of polymorphism:
Suppose if you are in class room that time you behave like a student, when you are in market at that time you behave like a customer, when you at your home at that time you behave like a son or daughter, Here one person present in different-different behaviors.
There are two types of polymorphism in java.
1) Method Overloading (Compile-time polymorphism)
2) Method Overriding (Run-time polymorphism)
1) Method Overloading
- Method Overloading is to define two or more methods of same name in a class, provided that there argument list or parameters are different.
- This increases the readability of the program.
- Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as addTwoNos(int,int) for two parameters, and addThreeNos(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behavior of the method because their name differs. So, we perform method overloading to figure out the program quickly.
In this example, we have created two overloaded methods, first sum method performs addition of two numbers and second sum method performs addition of three numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Calculations { public void sum(int a, int b) { System.out.println(a + b); } public void sum(int a, int b, int c) { System.out.println(a + b + c); } public static void main(String args[]) { Calculations obj = new Calculations(); obj.sum(10, 10, 10); obj.sum(20, 20); } } |
Example: In following example, we have created two overloaded methods that differs in data type. The first sum method receives two integer arguments and second sum method receives two double arguments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Calculations { void sum(int a, int b) { System.out.println(a + b); } void sum(double a, double b) { System.out.println(a + b); } public static void main(String args[]) { Calculations obj = new Calculations(); obj.sum(10.5, 10.5); obj.sum(20, 20); } } |
2) Method Overriding
- Child class has the same method as of base class. In such cases child class overrides the parent class method without even touching the source code of the base class.
- In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.
- Method overriding applies only to inherited methods.
- Overriding method can have different return type.
- Static and final methods cannot be overridden.
- Constructors cannot be overridden.
1 2 3 4 5 | class ParentClass{ public void status(){ System.out.println("Parent's Method"); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class ChildClass extends ParentClass { public void status() { System.out.println("Child's Method"); } public static void main(String args[]) { // Creating objects ParentClass parent = new ParentClass(); ParentClass parent_1 = new ChildClass(); ChildClass child = new ChildClass(); parent.status(); // This will print Parent's status method child.status(); // This will print child's status method parent_1.status(); // This will print child's status method } } |
Can we overload static method?
Yes. It is possible to overload static method in Java. Make sure they must have either different number of arguments, or different type of argument.
Can we override static method in Java?
No, you cannot override static method in Java because they are resolved and bonded during compile time.
very good vinod
ReplyDeleteHi,
ReplyDeleteNicely explained.
Can you also give real time example of polymorphism in the context of selenium, it is asked to me in interview.
Hi Vinod,
ReplyDeleteWell explained. It will be helpful if you can add some examples to support this
Very good Vinod
ReplyDelete