DEV Community

Cover image for πŸš€ Day 8 of My Automation Journey – Inheritance in Java (IS-A Relationship)
bala d kaveri
bala d kaveri

Posted on • Edited on

πŸš€ Day 8 of My Automation Journey – Inheritance in Java (IS-A Relationship)

On Day 8, I learned one of the most powerful concepts in Java and OOP:

βœ… Inheritance

This concept helps us reuse code and build real-world relationships between classes.

πŸ“Œ What is Inheritance?

Inheritance is a mechanism where:

A child class acquires the properties and behavior of a parent class.

In simple words:

Child class extends Parent class
Enter fullscreen mode Exit fullscreen mode

We use the keyword:

extends
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ When Do We Use Inheritance?

We use inheritance when there is an:

πŸ” IS-A relationship

Example:

Developer is an Employee

Car is a Vehicle

πŸ‘‰ In real life:
Every Developer is an Employee
But not every Employee is a Developer.

πŸ”Ή Program 1: Employee β†’ Developer
βœ… Parent Class

package inheritance;

public class Employee {

    int salary;

    void Welcome() {
        System.out.println("Welcome");
    }

    public static void main(String[] args) {

        Employee emp = new Employee();
        emp.salary = 20000;

        System.out.println(emp.salary);
        emp.Welcome();
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ… Child Class

package inheritance;

public class Devloper extends Employee {

    public static void main(String[] args) {

        Devloper dev = new Devloper();
        dev.salary = 10000;

        System.out.println(dev.salary);
        dev.Welcome();
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ” Step-by-Step Explanation
πŸ”Ή Step 1: extends Keyword

public class Devloper extends Employee
Enter fullscreen mode Exit fullscreen mode

This means:

Devloper automatically gets:

salary variable

Welcome() method

Without rewriting them.

πŸ”Ή Step 2: Object Creation

Devloper dev = new Devloper();
Enter fullscreen mode Exit fullscreen mode

When this line runs:

  1. Memory is allocated
  2. Devloper object is created
  3. It contains:
  • Its own properties (if any)
  • All Employee properties

πŸ”Ή Step 3: Accessing Parent Data

dev.salary = 10000;
Enter fullscreen mode Exit fullscreen mode

Even though salary is declared in Employee,
Devloper can use it because of inheritance.

πŸ”Ή Step 4: Calling Parent Method

dev.Welcome();
Enter fullscreen mode Exit fullscreen mode

Output:

Welcome
Enter fullscreen mode Exit fullscreen mode

The method is coming from the parent class.

🎯 Why Do We Use Inheritance?

Without inheritance:

❌ You must rewrite salary, id, name in every class
❌ Code duplication
❌ Hard to maintain

With inheritance:

βœ… Code reusability
βœ… Clean structure
βœ… Real-world modeling
βœ… Easy maintenance

πŸ”Ή Program 2: Vehicle β†’ Car

Let’s see another example.

βœ… Parent Class

package inheritance_1;

public class Vehicle {

    String Car, Bicycle, Lorry, Bus, truck;

    void Welcome() {
        System.out.println("Welcome to the vehicle World");
    }

    public static void main(String[] args) {

        Vehicle Model = new Vehicle();
        Model.Car = "Benz";
        Model.Bicycle = "Hercules";
        Model.Lorry = "TATA";

        Model.Welcome();
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ… Child Class

package inheritance_1;

public class Car extends Vehicle {

    public static void main(String[] args) {

        Car Model_1 = new Car();
        Model_1.Car = "bmw";

        System.out.println(Model_1.Car);
        Model_1.Welcome();
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ” What Happens Here?
When you create:

Car Model_1 = new Car();
Enter fullscreen mode Exit fullscreen mode

Java:

  1. Creates Car object
  2. Also includes all Vehicle properties
  3. Allows access to Welcome() method

Output:

bmw
Welcome to the vehicle World
Enter fullscreen mode Exit fullscreen mode

🧠 Memory Understanding

When a child object is created:

[ Employee Data ]
[ Devloper Data ]
Enter fullscreen mode Exit fullscreen mode

OR

[ Vehicle Data ]
[ Car Data ]
Enter fullscreen mode Exit fullscreen mode

Parent part is always present inside child object.

πŸ— Types of Inheritance in Java

1️⃣ Single Inheritance
2️⃣ Multilevel Inheritance
3️⃣ Hierarchical Inheritance

(Java does NOT support multiple inheritance with classes.)

⚑ Important Notes

βœ” Inheritance improves reusability
βœ” extends is used for classes
βœ” Child gets public & protected members
βœ” Constructors are not inherited (very important)

🎀 Real-World Thinking

Think like this:

Employee β†’ Developer
Vehicle β†’ Car
Animal β†’ Dog
Account β†’ SavingsAccount

This is how automation frameworks are designed.

In Selenium projects:

BaseClass β†’ TestClass
WebDriverSetup β†’ LoginTest
Utility β†’ TestCases

Inheritance is heavily used.

🏁 Day 8 Key Takeaways

βœ… Understood IS-A relationship
βœ… Learned extends keyword
βœ… Accessed parent variable in child
βœ… Called parent method from child
βœ… Saw real-world examples

πŸ‘¨β€πŸ« Trainer: Nantha from Payilagam

πŸ€– 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)