An interface defines a contract — what methods a class must implement, without providing the implementation. An abstract class is a partial implementation — some methods are complete, others are left for subclasses. In Selenium, WebDriver is an interface that ChromeDriver implements.
public interface Clickable {
void click(String locator);
boolean isClickable(String locator);
}
public interface Typeable {
void type(String locator, String text);
void clear(String locator);
}
// A class can implement multiple interfaces
public class WebElement implements Clickable, Typeable {
@Override
public void click(String locator) {
System.out.println("Clicking: " + locator);
}
@Override
public boolean isClickable(String locator) {
System.out.println("Checking clickable: " + locator);
return true;
}
@Override
public void type(String locator, String text) {
System.out.println("Typing \"" + text + "\" into " + locator);
}
@Override
public void clear(String locator) {
System.out.println("Clearing: " + locator);
}
}public abstract class BaseTest {
// Concrete method — shared by all tests
void openBrowser() {
System.out.println("Opening browser");
}
void closeBrowser() {
System.out.println("Closing browser");
}
// Abstract method — each test must define its own
abstract void runTest();
// Template method pattern
void execute() {
openBrowser();
runTest();
closeBrowser();
}
}
public class LoginTest extends BaseTest {
@Override
void runTest() {
System.out.println("Running login test");
}
}
public class SearchTest extends BaseTest {
@Override
void runTest() {
System.out.println("Running search test");
}
}public class AbstractDemo {
public static void main(String[] args) {
BaseTest login = new LoginTest();
login.execute();
System.out.println();
BaseTest search = new SearchTest();
search.execute();
// BaseTest base = new BaseTest(); // ERROR — cannot instantiate abstract class
}
}Opening browser
Running login test
Closing browser
Opening browser
Running search test
Closing browser| Feature | Interface | Abstract Class |
|---|---|---|
| Methods | All abstract (by default) | Mix of abstract and concrete |
| Fields | Only constants (static final) | Can have regular fields |
| Multiple | Can implement multiple | Can extend only one |
| Constructor | No | Yes |
| Use when | Defining a contract (WebDriver) | Sharing common code (BasePage) |
Q: What is the difference between an interface and an abstract class?
A: An interface defines a pure contract — only method signatures, no implementation (before Java 8). A class can implement multiple interfaces. An abstract class provides partial implementation — some methods are complete, others are abstract. A class can extend only one abstract class. Use interfaces to define capabilities (Clickable, Scrollable). Use abstract classes to share common code among related classes (BasePage, BaseTest).
Q: Can you instantiate an abstract class or an interface?
A: No. You cannot create an object of an abstract class or interface using new. You must create a concrete subclass that implements all abstract methods, then instantiate the subclass. However, you can declare a reference of the abstract type: WebDriver driver = new ChromeDriver() — WebDriver is an interface, ChromeDriver is the concrete implementation.
Exercise 1: Create an interface Searchable with a method search(String query). Implement it in GooglePage and AmazonPage classes. Write code that uses the Searchable type to call search on both.
Exercise 2: Create an abstract class Report with a concrete method header() and an abstract method generate(). Create HTMLReport and PDFReport subclasses. Call both through the parent type.