power_settings_new
When should I use interfaces instead of abstract classes with Selenium WebDriver in Java? Can you please exaplain with an example?

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

1 Answer

by (171 points)
0 votes
we use interface for user to create their own method out of it. User can implement that interface on any of their class.

E.g. WebDriver is an interface which is implemented by classes, RemoteWebDriver, FirefoxDriver, ChromeDriver and many more. So you can easily declare a variable of type WebDriver and initiate it with any of above classes.

You use interface when you want to enforce rule/create blue print of your architecture. Also you are allowing user of the interface to implement another interface as well.

e.g. RemoteWebDriver implements all below interfaces.

HasCapabilities, HasInputDevices, FindsByClassName, FindsByCssSelector, FindsById, FindsByLinkText, FindsByName, FindsByTagName, FindsByXPath, JavascriptExecutor, SearchContext, TakesScreenshot, WebDriver

 

When you make abstract class, you make sure that user extends that class, user should not create new instence of that class. Again that will act as a blue print for you.

E.g. BaseActions, BasePageObjcts. these classes will have implementation of common reusable methods and variables

Hope that helps.
add
...