Why we write WebDriver driver = new ChromeDriver(); in Selenium Web Driver?
In the previous article we saw, what are related interfaces and classes in Selenium Web Driver.
You can read that blog here
What is WebDriver interface & hierarchy of related interfaces and Classes
In our script first line was
System.setProperty(“webdriver.chrome.driver”,”<ChromeDriver_Path>/chromedriver”);
WebDriver driver = new ChromeDriver();
In this article we are going to see why we write like the way we have written and not
WebDriver driver = new WebDriver();
OR
ChromeDriver driver = new ChromeDriver();
WebDriver driver= new WebDriver();
As in the previous blog, we have seen WebDriver is an interface. We cannot create Object of an Interface. If we write code like this we will get compile time error

ChromeDriver driver = new ChromeDriver();
driver.get("https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/WebDriver.html");
Technically this will work. So you can start writing your code with this snippet.
But when we automate any web application, we must cover automation framework for all the supported browsers. It is called as cross browser testing
If you start your script with above statement you are creating instance of ChromeDriver class. As per the java concept if you create an object using New keyword it will initiate constructor of that particular class. We have object of ChromeDriver class and it will only work with Chrome browser
This will create a problem if you want to perform cross browser testing like writing script for different browsers like Firefox, Edge etc. You will have to create another instance of another browser using New keyword like
FirefoxDriver driver_1= new FirefoxDriver();
Instead we can create reference variable of WebDriver interface and cast it to browser class as below
WebDriver driver = new ChromeDriver();
Now you can write your script for any browser. You can simple change the definition statement as
WebDriver driver = new FirefoxDriver();
In this case script will execute without any issue. This driver instance has access to almost all the methods of WebDriver. This happens because every browser extends RemoteWebDriver and RemoteWebDriver implements WebDriver interface.
One more advantage of doing it is we can use same instance of driver with different browsers by quitting earlier session in same script. Below code will work absolutely fine
WebDriver driver;
//Use driver instance to execute test on Chrome browser
System.setProperty(“webdriver.chrome.driver”,”<ChromeDriver_Path>/chromedriver”);
driver = new ChromeDriver();
//test code
driver.quit()//Use same driver instance to execute test on Firefox browser
System.setProperty("webdriver.gecko.driver", "C:\\Drivers\\geckodriver.exe");
driver = new FirefoxDriver();
driver.quit()
Now you might have one question in your mind? If we are restricted to use method implemented inside WebDriver interface what if we want to use browser specific methods that means method present inside Browser Driver class
We can still use those methods whenever it required for execution on respective browser only. We can do it by casting web driver instance to the browser class driver. For eg. We have launchApp() present only inside ChromeDriver class.
So if we use this method in our code first it will give us compile time error.

Complete code will look like this,
WebDriver driver;
//Use driver instance to execute test on Chrome browser
System.setProperty(“webdriver.chrome.driver”,”<ChromeDriver_Path>/chromedriver”);
driver = new ChromeDriver();
//test code
driver.launchApp("")
;driver.quit()
On mouse hover it will give us suggestion to cast driver to ChromeDriver for this particular step

Once we click on suggestion it will cast driver to ChromeDriver class

Complete code will look like this
WebDriver driver;
System.setProperty(“webdriver.chrome.driver”,”<ChromeDriver_Path>/chromedriver”);
driver = new ChromeDriver();
((ChromeDriver) driver).launchApp("");
driver.quit();
Summary is, it is better to create reference of WebDriver interface and cast it to browser driver class than creating separate object of each class