Lab 5 Oop
Lab 5 Oop
OOP
Lab Manual: Implementation of the Concept of
Inheritance in Java
1. Objective
The objective of this lab is to:
2. Introduction to Inheritance
What is Inheritance?
Definition:
1. Code Reusability: Avoids writing the same code multiple times.
2. Better Organization: Helps in structuring code efficiently.
3. Extensibility: New features can be added easily without modifying existing
code.
4. Readability & Maintenance: Inherited code makes the program more
readable.
1. The Parent Class (Super Class): The class that is being inherited.
2. The Child Class (Sub Class): The class that inherits from another
class.
3. The extends keyword: Used to establish an inheritance
relationship.
Syntax of Inheritance in Java
class Parent {
// Parent class properties and methods
}
A class that inherits from another class and can add or modify functionalities.
6. What is a Constructor?
A constructor is a special method in a class that runs automatically when an object is
created.
Its name is always the same as the class name, and it initializes the object's values
Types of Constructors
8. Scenario-Based Examples
Scenario : Employee Management System
Problem Statement:
Create a class Employee with properties name and salary. Create a subclass Manager
that has an additional property bonus.
class UniversityEmployee {
String name;
double salary;
// Constructor jo name aur salary ko initialize karta hai.
UniversityEmployee(String name, double salary) {
this.name = name;
this.salary = salary;
}
void display() {
System.out.println("Name: " + name);
System.out.println("Salary: $" + salary);
}
}
class DepartmentManager extends UniversityEmployee {
double bonus;
// Constructor jo name, salary aur bonus ko initialize karega.
DepartmentManager(String name, double salary, double bonus) {
// Department manager ka ek additional attribute "bonus" hoga.
super(name, salary); // Super ka use parent class ka constructor call karne ke
liye.
this.bonus = bonus;
}
void display() {
super.display();
System.out.println("Bonus: $" + bonus);
}
}
public class UniversityExample {
public static void main(String[] args) {