DEV Community

Cover image for πŸš€ Day 18 of My Automation Journey – Inheritance QA's
bala d kaveri
bala d kaveri

Posted on

πŸš€ Day 18 of My Automation Journey – Inheritance QA's

In today’s session, I explored one of the core pillars of Object-Oriented Programming (OOP) β€” Inheritance.

Inheritance allows a class to reuse properties and methods from another class, which helps reduce code duplication and improve maintainability.

Today I practiced:

  • Basic inheritance concepts
  • Types of inheritance
  • Real-world coding scenarios
  • Tricky inheritance interview questions

Let’s go step by step.

πŸ”Ή 1. What is Inheritance?

Inheritance is a mechanism in Java where one class acquires the properties and behaviors of another class.

The class that is inherited from is called the Parent Class (Superclass) and the class that inherits is called the Child Class (Subclass).

Example:

class Parent {

}

class Child extends Parent {

}
Enter fullscreen mode Exit fullscreen mode

Here:

Parent β†’ Superclass

Child β†’ Subclass
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 2. Why is Inheritance Used?

Inheritance is mainly used for code reusability.

Benefits include:

βœ” Reusing existing code
βœ” Reducing duplication
βœ” Improving maintainability
βœ” Supporting polymorphism
βœ” Creating hierarchical relationships

Example:

Instead of writing deposit() method in multiple classes, we write it once in the parent class and reuse it.

πŸ”Ή 3. How to Achieve Inheritance?

Inheritance is achieved using the extends keyword.

Example:

class Animal {

    void eat() {
        System.out.println("Animal eats food");
    }

}

class Dog extends Animal {

}
Enter fullscreen mode Exit fullscreen mode

Here, the Dog class inherits the eat() method from Animal.

πŸ”Ή 4. How Many Types of Inheritance?

Java supports 5 types of inheritance conceptually.

However, not all are supported using classes.

πŸ”Ή 5. Types of Inheritance

| Type                     | Description                               |
| ------------------------ | ----------------------------------------- |
| Single Inheritance       | One child inherits from one parent        |
| Multilevel Inheritance   | A class inherits from a child class       |
| Hierarchical Inheritance | Multiple classes inherit from one parent  |
| Multiple Inheritance     | One class inherits from multiple parents  |
| Hybrid Inheritance       | Combination of multiple inheritance types |
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 6. Which Inheritance is Not Supported in Java?

Java does not support Multiple Inheritance using classes.

Example (Not allowed):

class A {

}

class B {

}

class C extends A, B {

}
Enter fullscreen mode Exit fullscreen mode

Reason:

This creates the Diamond Problem, causing ambiguity.

However, Java allows multiple inheritance using interfaces.

πŸ”Ή 7. Which Keyword is Used for Inheritance?

The keyword used is:

extends
Enter fullscreen mode Exit fullscreen mode

Example:

class Child extends Parent {

}
Enter fullscreen mode Exit fullscreen mode

πŸ’» Inheritance Scenario Examples

Now let’s look at real-world scenarios.
1.Banking System :
A Bank Account has common features like accountNumber, balance, deposit(), withdraw().
Different types of accounts such as Savings Account and Current Account have additional features.

Question:
Which class should be the parent, and which should be the child classes?
Enter fullscreen mode Exit fullscreen mode

🏦 Scenario 1 – Banking System
Parent Class

class BankAccount {

    int accountNumber;
    double balance;

    void deposit(double amount) {
        balance += amount;
        System.out.println("Deposited: " + amount);
    }

    void withdraw(double amount) {
        balance -= amount;
        System.out.println("Withdrawn: " + amount);
    }

}
Enter fullscreen mode Exit fullscreen mode

Child Class – Savings Account

class SavingsAccount extends BankAccount {

    double interestRate = 4.5;

    void showInterest() {
        System.out.println("Interest Rate: " + interestRate);
    }

}
Enter fullscreen mode Exit fullscreen mode

Child Class – Current Account

class CurrentAccount extends BankAccount {

    double overdraftLimit = 10000;

    void showOverdraft() {
        System.out.println("Overdraft Limit: " + overdraftLimit);
    }

}
Enter fullscreen mode Exit fullscreen mode

Parent Class β†’ BankAccount
Child Classes β†’ SavingsAccount, CurrentAccount

2.Vehicle System :
A Vehicle has common properties like speed, fuelType, start(), stop().
Specific vehicles like Car, Bike, and Truck have their own behaviors.

Question:
How can inheritance be used here?
Enter fullscreen mode Exit fullscreen mode

πŸš— Scenario 2 – Vehicle System
Parent Class

class Vehicle {

    int speed;
    String fuelType;

    void start() {
        System.out.println("Vehicle started");
    }

    void stop() {
        System.out.println("Vehicle stopped");
    }

}
Enter fullscreen mode Exit fullscreen mode

Child Classes

class Car extends Vehicle {

    void openTrunk() {
        System.out.println("Car trunk opened");
    }

}

class Bike extends Vehicle {

    void kickStart() {
        System.out.println("Bike started with kick");
    }

}

class Truck extends Vehicle {

    void loadGoods() {
        System.out.println("Truck loading goods");
    }

}
Enter fullscreen mode Exit fullscreen mode

Parent Class β†’ Vehicle
Child Classes β†’ Car, Bike, Truck

3.Employee Management :
In a company, Employee has common details like id, name, salary.
Different employees like Manager, Developer, and Tester have extra responsibilities.

Question:
Which class should be the base class?
Enter fullscreen mode Exit fullscreen mode

πŸ‘¨β€πŸ’Ό Scenario 3 – Employee Management System
Base Class

class Employee {

    int id;
    String name;
    double salary;

    void work() {
        System.out.println("Employee working");
    }

}
Enter fullscreen mode Exit fullscreen mode

Child Classes

class Manager extends Employee {

    void manageTeam() {
        System.out.println("Managing team");
    }

}

class Developer extends Employee {

    void writeCode() {
        System.out.println("Writing code");
    }

}

class Tester extends Employee {

    void testSoftware() {
        System.out.println("Testing application");
    }

}
Enter fullscreen mode Exit fullscreen mode

Base Class β†’ Employee

4.Online Shopping System :
A Product has common details like productId, price, description.
Different product types include Electronics, Clothing, and Books.

Question:
Which class should be the superclass, and which should be subclasses?
Enter fullscreen mode Exit fullscreen mode

πŸ›’ Scenario 4 – Online Shopping System
Superclass

class Product {

    int productId;
    double price;
    String description;

    void showProduct() {
        System.out.println("Product details");
    }

}
Enter fullscreen mode Exit fullscreen mode

Subclasses

class Electronics extends Product {

    int warranty;

}

class Clothing extends Product {

    String size;

}

class Books extends Product {

    String author;

}
Enter fullscreen mode Exit fullscreen mode

`Superclass β†’ Product

Subclasses β†’ Electronics, Clothing, Books`

🧠 Inheritance Tricky Questions

Let’s analyze some tricky inheritance cases.

Case 1 – Single Inheritance

class A {

}

class B extends A {

}
Enter fullscreen mode Exit fullscreen mode

βœ” Valid

Case 2 – Multilevel Inheritance

class A {

}

class B extends A {

}

class C extends B {

}
Enter fullscreen mode Exit fullscreen mode

βœ” Valid

Case 3 – Hierarchical Inheritance

class A {

}

class B extends A {

}

class C extends A {

}
Enter fullscreen mode Exit fullscreen mode

βœ” Valid

Case 4 – Multiple Inheritance

class A {

}

class B extends A {

}

class C extends B, A {

}

Enter fullscreen mode Exit fullscreen mode

❌ Invalid
Java does not support multiple inheritance using classes.

Case 5 – Multi-Level Chain

class A {

}

class B extends A {

}

class C extends B {

}

class D extends C {

}
Enter fullscreen mode Exit fullscreen mode

βœ” Valid

Case 6 – Hierarchical Structure

class A {

}

class B extends A {

}

class C extends B {

}

class D extends A {

}
Enter fullscreen mode Exit fullscreen mode

βœ” Valid

Case 7 – Final Class Inheritance

final class A {

}

class B extends A {

}
Enter fullscreen mode Exit fullscreen mode

❌ Invalid

A final class cannot be extended.

Case 8 – Final Parent Class

class A {

}

final class B extends A {

}

class C extends B {

}
Enter fullscreen mode Exit fullscreen mode

❌ Invalid

Because class B is final, it cannot be extended.

Case 9 – Multiple Parent Classes

class A {

}

class B {

}

class C extends A, B {

}

Enter fullscreen mode Exit fullscreen mode

❌ Invalid

Java does not allow extending multiple classes.

🧠 Key Takeaways

βœ” Inheritance helps reuse code
βœ” extends keyword is used to achieve inheritance
βœ” Java supports single, multilevel, and hierarchical inheritance
βœ” Multiple inheritance using classes is not supported
βœ” final classes cannot be extended
βœ” Inheritance forms the backbone of many Java frameworks

🏁 Conclusion

Inheritance is one of the most powerful concepts in Object-Oriented Programming.

It helps developers:

  • Build reusable code
  • Reduce duplication
  • Create logical relationships between classes

Understanding inheritance is very important when working with automation frameworks like Selenium, where classes often inherit common functionality.

This was Day 18 of my Automation Journey, and I’m excited to continue exploring more Java concepts.

πŸš€ See you in Day 19!

πŸ€– A Small Note
I used ChatGPT to help structure and refine this blog while ensuring the concepts remain aligned with my trainer’s explanations.

Top comments (0)