Why can't we write "ChromeDriver driver = new ChromeDriver();" in Selenium?
inna last edited by
Why do we create a ChromeDriver instance and then assign it to a "WebDriver" interace reference.
Webdriver driver = new ChromeDriver();
Why cant we create a Chromedriver , Firefoxdriver or IEDriver reference and assign it the object?
I am a complete newbie in Selenium and java?
carriann last edited by
Actually, you can.
ChromeDriver x = new ChromeDriver();
is a perfect valid Java statement.
The classes ChromeDriver and Firefoxdriver implement these methods for the specificity of these browser. However, for your usage it is not necessary to know about each implementation, because they simply fulfill the interface (a contract) - it is a principle know as Abstraction in OOP.
It allows you to pass a concrete object (a ChromeDriver or a Firefoxdriver) throughout methods "disguised" as something more generic. If you actually need to change the object, you just need to act of its creation point and everything else will work fine at run-time. If you come to need something specific of ChromeDriver, you can cast the WebDriver to a ChromeDriver, but note that this operation is fragile to the change of the concrete object.
In a nutshell, in programming, always prefer to act on higher levels of abstraction, in order to be robust against changes in the lower levels.