By Shivam Singh, founder of TesterRank · Updated September 2026
Inheritance lets one class acquire the fields and methods of another. The child class extends the parent class. In automation, LoginPage extends BasePage — LoginPage inherits common methods like click, type, and waitForElement from BasePage.
public class BasePage {
String driver;
BasePage(String driver) {
this.driver = driver;
}
void click(String locator) {
System.out.println("[" + driver + "] Click: " + locator);
}
void type(String locator, String text) {
System.out.println("[" + driver + "] Type \"" + text + "\" into " + locator);
}
String getText(String locator) {
System.out.println("[" + driver + "] Get text from: " + locator);
return "Sample Text";
}
}LoginPage extends BasePage, so it gets all of BasePage's methods for free and only adds what is special about login.
public class LoginPage extends BasePage {
LoginPage(String driver) {
super(driver); // call parent constructor
}
void login(String username, String password) {
type("#username", username); // inherited from BasePage
type("#password", password); // inherited from BasePage
click("#login-btn"); // inherited from BasePage
}
String getErrorMessage() {
return getText(".error-msg"); // inherited from BasePage
}
}Now use LoginPage. It can call both its own methods and the ones it inherited.
public class InheritanceDemo {
public static void main(String[] args) {
LoginPage loginPage = new LoginPage("ChromeDriver");
loginPage.login("testuser", "Test@123");
loginPage.getErrorMessage();
}
}The output shows the inherited methods doing the work:
[ChromeDriver] Type "testuser" into #username
[ChromeDriver] Type "Test@123" into #password
[ChromeDriver] Click: #login-btn
[ChromeDriver] Get text from: .error-msgsuper accesses the parent class. super() calls the parent constructor (must be the first line in the child constructor). super.method() calls the parent version of an overridden method.
Overriding means a child class replaces a parent method with its own version. Here LoginTest changes what setUp does.
public class BaseTest {
void setUp() {
System.out.println("Opening browser");
}
void tearDown() {
System.out.println("Closing browser");
}
}
public class LoginTest extends BaseTest {
@Override
void setUp() {
super.setUp(); // call parent setUp first
System.out.println("Navigating to login page");
}
void testValidLogin() {
System.out.println("Testing valid login");
}
public static void main(String[] args) {
LoginTest test = new LoginTest();
test.setUp();
test.testValidLogin();
test.tearDown();
}
}The output shows the child's version running, plus the parent's steps it still calls:
Opening browser
Navigating to login page
Testing valid login
Closing browserJava does not support multiple inheritance for classes — a class can extend only ONE parent. This prevents the "diamond problem". Use interfaces for multiple-type behavior.
Q: What is the difference between method overloading and method overriding?
A: Overloading: same method name, different parameters, within the same class. Resolved at compile time. Example: log(String msg) and log(String level, String msg). Overriding: same method name AND same parameters, in parent and child class. Resolved at runtime. The child version replaces the parent version. Marked with @Override annotation.
Q: What is the use of the super keyword?
A: super has two uses: (1) super() calls the parent class constructor — it must be the first line in the child constructor. (2) super.method() calls the parent version of a method when the child has overridden it. This is common in setUp/tearDown where the child adds steps on top of the parent implementation.
Exercise 1: Create a BasePage with methods click(locator) and type(locator, text). Create SearchPage extends BasePage with a search(query) method that uses the inherited type and click methods.
Exercise 2: Create Animal with a speak() method. Create Dog and Cat that extend Animal and override speak(). Create an array of Animal references holding Dog and Cat objects, loop through and call speak() on each.