๐ 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)
| Time | Activity |
|---|---|
| 0โ15 min | Concept understanding |
| 15โ45 min | Single Inheritance Program |
| 45โ75 min | Multilevel Inheritance Program |
| 75โ100 min | Method Overriding |
| 100โ120 min | Constructors 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
Personis the parent classEmployeeis the child classEmployeeinherits variables and methods fromPerson- Object of
Employeecan 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:
salaryin 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 classEmployeeโ inherits from PersonManagerโ 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:
salaryin Employeebonusin 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