6-๐Ÿงช Java Lab Session (2 Hours) โ€“ Inheritance, Method Overriding & Constructor Chaining

๐ŸŒŸ Introduction

In this lab session, you will work with some of the most important concepts of Java:

  • Inheritance
  • Multilevel Inheritance
  • Method Overriding
  • Constructors in Inheritance

This session is designed to help you understand concepts through hands-on coding. By writing and running programs, you will learn how Java uses inheritance to build structured and reusable applications.

By the end of this lab, you will be able to:

โœ” Create inheritance relationships
โœ” Use multilevel inheritance
โœ” Override methods
โœ” Understand constructor execution flow


โฑ๏ธ Lab Structure (2 Hours)

TimeActivity
0โ€“15 minConcept understanding
15โ€“45 minSingle Inheritance Program
45โ€“75 minMultilevel Inheritance Program
75โ€“100 minMethod Overriding
100โ€“120 minConstructors in Inheritance

๐Ÿงช Part 1: Single Inheritance


๐ŸŽฏ Program: Employee inherits Person


๐Ÿ’ป Code

class Person {

    String name;
    int age;

    void display() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }

}

class Employee extends Person {

    int empId;

    void showEmployeeId() {
        System.out.println("Employee ID: " + empId);
    }

}

public class Main {

    public static void main(String[] args) {

        Employee e = new Employee();

        e.name = "Ajay";
        e.age = 25;
        e.empId = 101;

        e.display();
        e.showEmployeeId();

    }

}

๐Ÿ”Ž Understanding the Program

  • Person is the parent class
  • Employee is the child class
  • Employee inherits variables and methods from Person
  • Object of Employee can access both parent and child members

๐Ÿงพ Output

Name: Ajay
Age: 25
Employee ID: 101

๐Ÿง  Key Concept

๐Ÿ‘‰ A child class can reuse properties of a parent class using extends.


๐Ÿ”ง Practice

Modify the program by adding:

  • salary in Employee
  • Print salary along with other details

๐Ÿงช Part 2: Multilevel Inheritance


๐ŸŽฏ Program: Person โ†’ Employee โ†’ Manager


๐Ÿ’ป Code

class Person {

    String name;

    void showName() {
        System.out.println("Name: " + name);
    }

}

class Employee extends Person {

    int empId;

    void showEmployeeId() {
        System.out.println("Employee ID: " + empId);
    }

}

class Manager extends Employee {

    String department;

    void showDepartment() {
        System.out.println("Department: " + department);
    }

}

public class Main {

    public static void main(String[] args) {

        Manager m = new Manager();

        m.name = "Ajay";
        m.empId = 200;
        m.department = "IT";

        m.showName();
        m.showEmployeeId();
        m.showDepartment();

    }

}

๐Ÿ”Ž Understanding the Program

  • Person โ†’ Base class
  • Employee โ†’ inherits from Person
  • Manager โ†’ inherits from Employee

๐Ÿ‘‰ Manager gets access to:

  • name
  • empId
  • department
  • all methods

๐Ÿงพ Output

Name: Ajay
Employee ID: 200
Department: IT

๐Ÿง  Key Concept

๐Ÿ‘‰ Multilevel inheritance allows a class to inherit features from multiple levels of parent classes.


๐Ÿ”ง Practice

Enhance the program by adding:

  • salary in Employee
  • bonus in Manager
  • Display all values

๐Ÿงช Part 3: Method Overriding


๐ŸŽฏ Program: Salary Calculation


๐Ÿ’ป Code

class Employee {

    void calculateSalary() {
        System.out.println("Basic Salary");
    }

}

class Manager extends Employee {

    @Override
    void calculateSalary() {
        System.out.println("Manager Salary with Bonus");
    }

}

public class Main {

    public static void main(String[] args) {

        Employee e = new Manager();
        e.calculateSalary();

    }

}

๐Ÿ”Ž Understanding the Program

  • calculateSalary() is defined in parent class
  • Child class provides its own implementation
  • This is called method overriding

๐Ÿงพ Output

Manager Salary with Bonus

๐Ÿง  Key Concept

๐Ÿ‘‰ Method call is decided at runtime, not compile time.

This is called:

๐Ÿ‘‰ Runtime Polymorphism


๐Ÿ”ง Practice

Create a new class:

  • Intern
  • Override calculateSalary() with different output

๐Ÿงช Part 4: Constructors in Inheritance


๐ŸŽฏ Program: Constructor Chaining


๐Ÿ’ป Code

class Person {

    Person() {
        System.out.println("Person Constructor");
    }

}

class Employee extends Person {

    Employee() {
        System.out.println("Employee Constructor");
    }

}

class Manager extends Employee {

    Manager() {
        System.out.println("Manager Constructor");
    }

}

public class Main {

    public static void main(String[] args) {

        Manager m = new Manager();

    }

}

๐Ÿ”Ž Understanding the Program

When object is created:

Manager m = new Manager();

Execution order:

Person โ†’ Employee โ†’ Manager

๐Ÿงพ Output

Person Constructor
Employee Constructor
Manager Constructor

๐Ÿง  Key Concept

๐Ÿ‘‰ Constructors execute from parent to child


๐Ÿ”ง Practice

Modify the program:

  • Add parameterized constructor
  • Use super()

๐ŸŽฏ Final Combined Practice

Create a program with:

  • Person โ†’ Employee โ†’ Manager
  • Constructor chaining
  • Method overriding

๐Ÿง  Key Takeaways

โœ” Inheritance helps in code reuse
โœ” Multilevel inheritance builds hierarchy
โœ” Method overriding enables dynamic behavior
โœ” Constructors execute in chain (parent โ†’ child)
โœ” Runtime polymorphism decides method execution


๐Ÿ Conclusion

This lab session provided hands-on experience with core inheritance concepts in Java. By implementing these programs, you now understand how classes are connected, how behavior can be modified, and how objects are initialized.

These concepts form the foundation of object-oriented programming and are essential for:

  • Writing efficient Java programs
  • Building real-world applications
  • Preparing for technical interviews

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *