RemoteWebDriver
is a concrete class that implements interface WebDriver
.
RemoteWebDriver
class contains additional methods that are not declared by interface WebDriver
. The method 'getSessionId()'
is one of them.
Hence, your object needed to be explicitly downcasted to use getSessionId
method since WebDriver
itself does not have knowledge of any method or variable which is purely defined by RemoteWebDriver
.
Coming to the question - "why shouldn't I just use RemoteWebDriver
instead of WebDriver
?"
Yes you may use RemoteWebDriver
instead of WebDriver
, however it makes the code non-compliant with the design principle - 'Code to the interface'
Your code will work fine though without any issues.
However, it will not have flexibility to use other driver implementations that may come in future (though very unlikely) which implements WebDriver
but does not extend RemoteWebdriver
. In such case, a variable of type RemoteWebDriver
cannot be assigned to an object of the class is which WebDriver's
implementation but not extending RemoteWebDriver