Answer
Abstract Class and Interface in Java
Abstract Class
Java
// Abstract class — can have abstract + concrete methods
abstract class Browser {
String name; // instance variable
String version;
Browser(String name) { // constructor allowed
this.name = name;
}
// Abstract method — no body, subclass MUST implement
abstract void launch();
abstract void close();
// Concrete method — already implemented
void takescreenshot() {
System.out.println("[" + name + "] Screenshot taken");
}
void printInfo() {
System.out.println("Browser: " + name);
}
}
class Chrome extends Browser {
Chrome() { super("Chrome"); }
@Override
public void launch() { System.out.println("Chrome launched"); }
@Override
public void close() { System.out.println("Chrome closed"); }
}
class Firefox extends Browser {
Firefox() { super("Firefox"); }
@Override
public void launch() { System.out.println("Firefox launched"); }
@Override
public void close() { System.out.println("Firefox closed"); }
}
public class AbstractDemo {
public static void main(String[] args) {
Browser b1 = new Chrome();
b1.launch();
b1.takescreenshot();
b1.close();
Browser b2 = new Firefox();
b2.launch();
b2.printInfo();
b2.close();
}
}
Output
CODE
Chrome launched
[Chrome] Screenshot taken
Chrome closed
Firefox launched
Browser: Firefox
Firefox closed
Interface
Java
// Interface — pure contract (all methods are public abstract by default)
interface Clickable {
void click();
void doubleClick();
// Java 8: default method (has body)
default void highlight() {
System.out.println("Element highlighted");
}
// Java 8: static method
static void printInfo() {
System.out.println("Clickable interface");
}
}
interface Typeable {
void type(String text);
void clear();
}
// Class implementing MULTIPLE interfaces
class TextBox implements Clickable, Typeable {
String locator;
TextBox(String locator) { this.locator = locator; }
@Override public void click() { System.out.println("Clicked " + locator); }
@Override public void doubleClick() { System.out.println("Double-clicked " + locator); }
@Override public void type(String text) { System.out.println("Typed '" + text + "' into " + locator); }
@Override public void clear() { System.out.println("Cleared " + locator); }
}
TextBox input = new TextBox("username-field");
input.click();
input.clear();
input.type("admin@test.com");
input.highlight(); // default method
Clickable.printInfo(); // static method
Abstract Class vs Interface
| Abstract Class | Interface | |
|---|---|---|
| Keyword | abstract class | interface |
| Methods | Abstract + Concrete | Abstract (default/static in Java 8+) |
| Variables | Any type | public static final only |
| Constructor | Yes | No |
| Inheritance | extends (one only) | implements (multiple allowed) |
| Access modifiers | Any | public only |
| Use when | Base class with shared code | Contract/capability for unrelated classes |
When to Use Each
CODE
Abstract Class:
✓ Classes share common code (e.g., BasePage with driver, wait)
✓ "IS-A" relationship (Chrome IS-A Browser)
✓ Need constructor logic
Interface:
✓ Unrelated classes share a contract (e.g., all clickable elements)
✓ Need multiple inheritance
✓ "CAN-DO" / capability relationship
✓ Defining API contracts
Real Automation Example
Java
// Abstract BasePage — shared code for all Page Objects
abstract class BasePage {
protected WebDriver driver;
protected WebDriverWait wait;
BasePage(WebDriver driver) {
this.driver = driver;
this.wait = new WebDriverWait(driver, Duration.ofSeconds(10));
}
protected WebElement waitForElement(By locator) {
return wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
}
abstract void verifyPageLoaded(); // each page must verify its own load
}
