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
We use the keyword:
extends
π 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();
}
}
β
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();
}
}
π Step-by-Step Explanation
πΉ Step 1: extends Keyword
public class Devloper extends Employee
This means:
Devloper automatically gets:
salary variable
Welcome() method
Without rewriting them.
πΉ Step 2: Object Creation
Devloper dev = new Devloper();
When this line runs:
- Memory is allocated
- Devloper object is created
- It contains:
- Its own properties (if any)
- All Employee properties
πΉ Step 3: Accessing Parent Data
dev.salary = 10000;
Even though salary is declared in Employee,
Devloper can use it because of inheritance.
πΉ Step 4: Calling Parent Method
dev.Welcome();
Output:
Welcome
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();
}
}
β
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();
}
}
π What Happens Here?
When you create:
Car Model_1 = new Car();
Java:
- Creates Car object
- Also includes all Vehicle properties
- Allows access to Welcome() method
Output:
bmw
Welcome to the vehicle World
π§ Memory Understanding
When a child object is created:
[ Employee Data ]
[ Devloper Data ]
OR
[ Vehicle Data ]
[ Car Data ]
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)