I have started learning Java and selenium and I would like to understand the difference between the below two set of code, and both of them executed fine and have the same behavior.
WebDriver is an interface and it is implemented by a class FirefoxDriver which has implemented all its methods. Hence to invoke methods present in FirefoxDriver we are creating a firefox object with it's constructor but why often people use first one (in below set) and what does it means when we use a constructor of a the class for creating the object but instead of that class name we put the interface name(which the class has implemented).
// First One
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FirstOne
{
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
System.out.println(driver.getTitle());
driver.close();
}
}
// Second one
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FirstOne
{
public static void main(String[] args)
{
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://google.com");
System.out.println(driver.getTitle());
driver.close();
}
}