Selenium Webdriver Day 5 : Polymorphism
Basically, Poly means many and morph means from, so polymorphism refers to a programming language's ability to process objects differently depending on their data types or class. Polymorphism is thus the ability of different classes of objects to respond to the same message in different, class specific ways. In this polymorphic methods have one name but different implementation for different classes.
Example : Suppose their is a base class named Shape and this having some method to calculate getArea(), now multiple derived classes are extending base classes like (Circle, Rectangle etc).All the derived classes are having same getArea() method to calculate area on the basis of their properties. To achieve this all the derived classes must override base class getArea() method.
Method overriding forms the basis for one of the java's most powerful concept : Dynamic method dispatch or lookup. Dynamic method dispatch is a process by which a call to an overridden method is performed at runtime, tather then compile time. Principle is , the superclass reference variable can hold a subclass object.
Example 1 :
// create new class
Output of example 2 will be : Turning bike to left side
Example : Suppose their is a base class named Shape and this having some method to calculate getArea(), now multiple derived classes are extending base classes like (Circle, Rectangle etc).All the derived classes are having same getArea() method to calculate area on the basis of their properties. To achieve this all the derived classes must override base class getArea() method.
Method overriding forms the basis for one of the java's most powerful concept : Dynamic method dispatch or lookup. Dynamic method dispatch is a process by which a call to an overridden method is performed at runtime, tather then compile time. Principle is , the superclass reference variable can hold a subclass object.
Example 1 :
package polymorphism_demo;
public class Area_calculation {
public void area(int a,int b) {
System.out.println("Area of Rectangle is ="+a*b);
}
public void area(int a) {
System.out.println("Area of Square is ="+a*a);
}
public static void main(String[] args) {
Area_calculation a=new Area_calculation();
//calculating area of rectangle
a.area(10, 20);
//Calculating area of square
a.area(10);
}
}
In the above code i have created two methods with same name area() but different datatypes and when i call these object from main method it will find out the respective methods by checking the value we have passed while calling. When i passed two value inside the area() method it calculate area of Rectangle and in second time when i passed single value it calculate area of Square.
Example 2 : First class is
package polymorphism_demo;Second class is :
public class Vehicle {
public void turnLeft() {
System.out.println("Turn vehicle to left side");
}
}
// create new class
package polymorphism_demo;Here i have created a base class Vehicle which contain method turnLeft(), i have also created derived class Bike which is inheriting base class and overriding the method turnLeft(). Now when overridden method is called through a superclass reference, java determines which version of that method to execute based on the type of object being referred to at the time of the call. In this we have created refrence variable of base class and called the getArea() method derived class.
public class Bike extends Vehicle{
public void turnLeft() {
System.out.println("Turning bike to left side");
}
public static void main(String[] args) {
Vehicle v=new Bike();
v.turnLeft();
}
}
Output of example 2 will be : Turning bike to left side