Sunday, 4 November 2018

Polymorphism in Selenium

Polymorphism


Def:-Performing tasks in different ways/ ability of an object to take on many forms.
     - Polymorphism derived from two Greek words,
     Poly-means Many
     Morphs -means forms/ways
     -So Polymorphism means many ways.
Two types of Polymorphism in Java


a) Compile Time Polymorphism---Method Overloading 
b) Run-time Polymorphism---Method Overriding



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:
Example:-1
You are in class room that time you behave like a student, 
You are in market at that time you behave like a customer, 
You at your home at that time you behave like a son or daughter, 

Example:-2
You are Son to your Father
You are Father to your Son
You are Brother to your Sister
You are Spouse to your wife 


In above Examples  one person present in different behaviors.

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, 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.

Example:
In this example, we have created two overloaded methods, 
first sum method performs addition of two numbers 
second sum method performs addition of three numbers.
------------------------------------------------------------------

class Calculations {
//Method1
 public void sum(int a, int b) 
 {
  System.out.println(a + b);
 }

//Method2
 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);
 }
 
                   } 
--------------------------------------------------------------
--------------------------------------------------------------
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.

Runtime Polymorphism in selenium



In Selenium we use runtime polymorphism concept.
It shows that we can execute same script on any browser.

To achieve runtime polymorphism following steps are mandatory-

  1. Inheritance (firefoxdriver is inheriting from webdriver)
  2. Method overriding (All the methods present in webdriver is overrided, As it is an interface)
  3. Upcasting (casting the object to a supertype)
     webdriver driver=new FirefoxDriver();

Example:-1

 Program for runtime polymorphism

package webdrivercommands;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Demo {
 //Method-1
  public static void testmethod(WebDriver driver) throws InterruptedException 
  {
   
 driver.get("https://goo.gl/maps/oLdAYyMZx3G2");
 System.out.println(driver.getTitle());

 driver.navigate().to("https://saiprasanthadabala.wordpress.com/");
 System.out.println(driver.getTitle());

 driver.navigate().back();
 Thread.sleep(2000);

 driver.navigate().forward();
 Thread.sleep(2000);

 driver.navigate().refresh();
 Thread.sleep(2000);

 driver.quit();
  }


  public static void main(String[] args) throws InterruptedException 
  {
   System.setProperty("webdriver.gecko.driver","D://Silpa//geckodriver.exe");
   WebDriver driver=new FirefoxDriver();
   testmethod(driver);
   System.setProperty("webdriver.chrome.driver","D://Silpa//chromedriver.exe");
   testmethod(new ChromeDriver());//Auto upcast
  }

}
------------------------------
Example:-2
//Class1
class ParentClass{  
    public void status(){
         System.out.println("Parent's Method");
       }  
}  
-----

//Class2
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
 }
}

No comments:

Post a Comment