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 {
}
Here:
Parent β Superclass
Child β Subclass
πΉ 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 {
}
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 |
πΉ 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 {
}
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
Example:
class Child extends Parent {
}
π» 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?
π¦ 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);
}
}
Child Class β Savings Account
class SavingsAccount extends BankAccount {
double interestRate = 4.5;
void showInterest() {
System.out.println("Interest Rate: " + interestRate);
}
}
Child Class β Current Account
class CurrentAccount extends BankAccount {
double overdraftLimit = 10000;
void showOverdraft() {
System.out.println("Overdraft Limit: " + overdraftLimit);
}
}
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?
π 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");
}
}
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");
}
}
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?
π¨βπΌ Scenario 3 β Employee Management System
Base Class
class Employee {
int id;
String name;
double salary;
void work() {
System.out.println("Employee working");
}
}
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");
}
}
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?
π Scenario 4 β Online Shopping System
Superclass
class Product {
int productId;
double price;
String description;
void showProduct() {
System.out.println("Product details");
}
}
Subclasses
class Electronics extends Product {
int warranty;
}
class Clothing extends Product {
String size;
}
class Books extends Product {
String author;
}
`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 {
}
β Valid
Case 2 β Multilevel Inheritance
class A {
}
class B extends A {
}
class C extends B {
}
β Valid
Case 3 β Hierarchical Inheritance
class A {
}
class B extends A {
}
class C extends A {
}
β Valid
Case 4 β Multiple Inheritance
class A {
}
class B extends A {
}
class C extends B, A {
}
β 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 {
}
β Valid
Case 6 β Hierarchical Structure
class A {
}
class B extends A {
}
class C extends B {
}
class D extends A {
}
β Valid
Case 7 β Final Class Inheritance
final class A {
}
class B extends A {
}
β Invalid
A final class cannot be extended.
Case 8 β Final Parent Class
class A {
}
final class B extends A {
}
class C extends B {
}
β Invalid
Because class B is final, it cannot be extended.
Case 9 β Multiple Parent Classes
class A {
}
class B {
}
class C extends A, B {
}
β 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)