0% found this document useful (0 votes)
2 views10 pages

Java Inheritance Case Studies

The document presents three case studies demonstrating different types of inheritance in Java: Banking System (Inheritance), Employee System (Single Inheritance), and Student Education System (Multilevel Inheritance). Each case study outlines the problem statement, objectives, system design, Java implementation, advantages, limitations, and concludes with the effectiveness of the inheritance model used. The examples illustrate how inheritance promotes code reusability, maintainability, and a clear hierarchical structure in software design.

Uploaded by

talhaaamir441
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Java Inheritance Case Studies

The document presents three case studies demonstrating different types of inheritance in Java: Banking System (Inheritance), Employee System (Single Inheritance), and Student Education System (Multilevel Inheritance). Each case study outlines the problem statement, objectives, system design, Java implementation, advantages, limitations, and concludes with the effectiveness of the inheritance model used. The examples illustrate how inheritance promotes code reusability, maintainability, and a clear hierarchical structure in software design.

Uploaded by

talhaaamir441
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Case Study 1: Banking System (Inheritance in Java)

1. Problem Statement
A bank needs to manage different types of accounts such as Savings Account and
Current Account.
All accounts share common properties like account number and balance, and common
operations like deposit and withdrawal.
However, each account type also has specific features:
 Savings Account → Interest calculation
 Current Account → Overdraft facility
To avoid code duplication and ensure better design, Inheritance in Java is used.

2. Objective
 To implement a banking system using Inheritance
 To demonstrate code reusability and extensibility
 To model real-world entities using OOP concepts

3. Concept of Inheritance
Inheritance is an OOP concept where one class acquires properties and methods of
another class.
 Superclass (Parent Class): Account
 Subclass (Child Classes):
o SavingsAccount
o CurrentAccount
👉 Syntax:
class Subclass extends Superclass

4. System Design (Class Structure)


Superclass: Account
Common attributes and behaviors:
 Data Members:
o accountNumber
o balance
 Methods:
o deposit()
o withdraw()
o display()
Subclass: SavingsAccount
Special Feature:
 Interest calculation
 Method:
o calculateInterest()
Subclass: CurrentAccount
Special Feature:
 Overdraft facility
 Method:
o checkOverdraft()
5. Java Implementation
// Superclass
class Account {
int accountNumber;
double balance;

// Constructor
Account(int accountNumber, double balance) {
[Link] = accountNumber;
[Link] = balance;
}

// Deposit method
void deposit(double amount) {
balance += amount;
[Link]("Deposited Amount: " + amount);
}

// Withdraw method
void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
[Link]("Withdrawn Amount: " + amount);
} else {
[Link]("Insufficient Balance!");
}
}

// Display details
void display() {
[Link]("Account Number: " + accountNumber);
[Link]("Balance: " + balance);
}
}

// Subclass: SavingsAccount
class SavingsAccount extends Account {
double interestRate = 5.0;

SavingsAccount(int accountNumber, double balance) {


super(accountNumber, balance); // calling parent constructor
}

void calculateInterest() {
double interest = (balance * interestRate) / 100;
[Link]("Interest Earned: " + interest);
}
}

// Subclass: CurrentAccount
class CurrentAccount extends Account {
double overdraftLimit = 10000;

CurrentAccount(int accountNumber, double balance) {


super(accountNumber, balance);
}
void checkOverdraft(double amount) {
if (amount <= (balance + overdraftLimit)) {
balance -= amount;
[Link]("Withdrawal with overdraft allowed");
} else {
[Link]("Overdraft limit exceeded!");
}
}
}

// Main Class
public class BankSystem {
public static void main(String[] args) {

// Savings Account Object


SavingsAccount sa = new SavingsAccount(101, 5000);
[Link](2000);
[Link]();
[Link]();

[Link]("---------------------");

// Current Account Object


CurrentAccount ca = new CurrentAccount(102, 3000);
[Link](8000);
[Link]();
}
}

6. Explanation of Code
Step 1: Superclass Creation
 Account class contains common attributes and methods
 Constructor initializes account details
Step 2: Inheritance
 SavingsAccount and CurrentAccount use:
extends Account
 This allows them to reuse all methods of Account
Step 3: Constructor Chaining
 super() is used to call parent constructor
Step 4: Method Extension
 Child classes add their own specific methods:
o calculateInterest()
o checkOverdraft()

7. Sample Output
Deposited Amount: 2000
Interest Earned: 350.0
Account Number: 101
Balance: 7000.0
---------------------
Withdrawal with overdraft allowed
Account Number: 102
Balance: -5000.0

8. Advantages of Inheritance
 Code Reusability → Common code written once
 Maintainability → Easy to update
 Extensibility → New account types can be added easily
 Hierarchical Classification → Clear structure

9. Limitations
 Tight coupling between classes
 Changes in parent class may affect child classes
 Improper design may reduce flexibility

10. Real-World Mapping


 Account → Generic bank account
 SavingsAccount → Interest-based account
 CurrentAccount → Business account with overdraft

11. Conclusion
The banking system effectively demonstrates Inheritance in Java, where common
functionalities are defined in a base class and specialized behaviors are implemented in
derived classes. This approach leads to clean, modular, and scalable software design.
Case Study: Employee System (Single Inheritance in Java)
1. Problem Statement
A company manages different types of employees.
All employees have common details such as employee ID, name, and salary.
A Manager is a specialized type of employee who receives an additional bonus.
To avoid redundancy and improve code organization, Single Inheritance is used.

2. Objective
 To demonstrate Single Inheritance
 To reuse common employee properties
 To extend functionality for a Manager

3. Concept: Single Inheritance


Single inheritance means one child class inherits from one parent class.
 Parent Class: Employee
 Child Class: Manager
👉 Relationship:
Employee → Manager

4. System Design
Superclass: Employee
Attributes:
 empId
 name
 salary
Methods:
 displayDetails()
 calculateSalary()
Subclass: Manager
Additional Attribute:
 bonus
Additional Method:
 calculateTotalSalary()

5. Java Implementation
// Superclass
class Employee {
int empId;
String name;
double salary;

// Constructor
Employee(int empId, String name, double salary) {
[Link] = empId;
[Link] = name;
[Link] = salary;
}

// Display employee details


void displayDetails() {
[Link]("Employee ID: " + empId);
[Link]("Name: " + name);
[Link]("Base Salary: " + salary);
}
}

// Subclass (Single Inheritance)


class Manager extends Employee {
double bonus;

// Constructor
Manager(int empId, String name, double salary, double bonus) {
super(empId, name, salary); // calling parent constructor
[Link] = bonus;
}

// Calculate total salary


void calculateTotalSalary() {
double totalSalary = salary + bonus;
[Link]("Bonus: " + bonus);
[Link]("Total Salary: " + totalSalary);
}
}

// Main Class
public class EmployeeSystem {
public static void main(String[] args) {

// Creating Manager object


Manager m = new Manager(101, "Shruti", 50000, 10000);

[Link](); // inherited method


[Link](); // subclass method
}
}

6. Explanation of Code
Step 1: Parent Class
 Employee contains common properties and method displayDetails()
Step 2: Child Class
 Manager extends Employee using:
extends Employee
Step 3: Constructor Chaining
 super() is used to initialize parent class variables
Step 4: Additional Functionality
 Manager adds bonus and calculates total salary
7. Sample Output
Employee ID: 101
Name: Shruti
Base Salary: 50000.0
Bonus: 10000.0
Total Salary: 60000.0

8. Advantages of Single Inheritance


 Promotes code reuse
 Improves readability
 Reduces duplication
 Easy to maintain and extend

9. Limitations
 Limited flexibility compared to multiple inheritance
 Tight dependency on parent class

10. Real-World Mapping


 Employee → General employee in a company
 Manager → Employee with additional responsibilities and bonus

11. Conclusion
The Employee System demonstrates Single Inheritance, where the Manager class
inherits common properties from the Employee class and extends it by adding bonus
functionality. This results in a clean, efficient, and scalable design.
Case Study: Student Education System (Multilevel Inheritance in Java)
1. Problem Statement
An educational institution manages student information at different levels.
 A Student has basic details like roll number and name
 A Test is a type of student that contains marks of subjects
 A Result is derived from Test and calculates the total and percentage
To represent this hierarchical relationship efficiently, Multilevel Inheritance is used.

2. Objective
 To demonstrate Multilevel Inheritance
 To show step-by-step extension of classes
 To reuse and extend functionality across multiple levels

3. Concept: Multilevel Inheritance


Multilevel inheritance means a class is derived from another derived class.
👉 Hierarchy:
Student → Test → Result
 Level 1 (Base Class): Student
 Level 2 (Intermediate Class): Test
 Level 3 (Derived Class): Result

4. System Design
Class 1: Student (Base Class)
Attributes:
 rollNo
 name
Method:
 displayStudentDetails()
Class 2: Test (Derived from Student)
Additional Attributes:
 marks1
 marks2
Method:
 displayMarks()
Class 3: Result (Derived from Test)
Functionality:
 Calculate total and percentage
Method:
 calculateResult()

5. Java Implementation
// Base Class
class Student {
int rollNo;
String name;
// Constructor
Student(int rollNo, String name) {
[Link] = rollNo;
[Link] = name;
}

void displayStudentDetails() {
[Link]("Roll No: " + rollNo);
[Link]("Name: " + name);
}
}

// Intermediate Class
class Test extends Student {
int marks1, marks2;

Test(int rollNo, String name, int marks1, int marks2) {


super(rollNo, name);
this.marks1 = marks1;
this.marks2 = marks2;
}

void displayMarks() {
[Link]("Marks in Subject 1: " + marks1);
[Link]("Marks in Subject 2: " + marks2);
}
}

// Derived Class
class Result extends Test {

Result(int rollNo, String name, int marks1, int marks2) {


super(rollNo, name, marks1, marks2);
}

void calculateResult() {
int total = marks1 + marks2;
double percentage = total / 2.0;

[Link]("Total Marks: " + total);


[Link]("Percentage: " + percentage + "%");
}
}

// Main Class
public class StudentSystem {
public static void main(String[] args) {

Result r = new Result(1, "Shruti", 85, 90);

[Link](); // from Student


[Link](); // from Test
[Link](); // from Result
}
}
6. Explanation of Code
Step 1: Base Class (Student)
 Contains basic student details
 Method to display student information
Step 2: Intermediate Class (Test)
 Inherits from Student
 Adds subject marks
 Uses super() to initialize parent attributes
Step 3: Derived Class (Result)
 Inherits from Test
 Calculates total and percentage
Step 4: Method Access
 Object of Result can access:
o Student methods
o Test methods
o Its own methods

7. Sample Output
Roll No: 1
Name: Shruti
Marks in Subject 1: 85
Marks in Subject 2: 90
Total Marks: 175
Percentage: 87.5%

8. Advantages of Multilevel Inheritance


 Step-by-step code reuse
 Logical hierarchical structure
 Improves modularity
 Easy to extend further

9. Limitations
 Increased complexity
 Difficult debugging if chain is long
 Changes in base class affect all derived classes

10. Real-World Mapping


 Student → Basic student info
 Test → Academic performance (marks)
 Result → Final evaluation

11. Conclusion
The Student Education System demonstrates Multilevel Inheritance, where classes are
built in a chain to extend functionality step-by-step. This results in a well-structured,
reusable, and scalable system design.

You might also like