OOP THROUGH JAVA – WEEK-4 LAB EXPERIMENTS
EXPERIMENT 1:
Title: Implementation of Method Overriding
Problem Statement
Design and implement a Java program to demonstrate method overriding using a real-time employee
salary management system. Create a parent class Employee containing a method calculateSalary().
Derive FullTimeEmployee and PartTimeEmployee from Employee. Override the calculateSalary()
method in each subclass to calculate salary according to the employee type. Create objects of the
different employee classes and demonstrate how the same method performs different operations in
different subclasses.
Implementation
class Employee
{
String employeeName;
Employee(String employeeName)
{
[Link] = employeeName;
}
void calculateSalary()
{
[Link]("Salary calculation for employee");
}
}
class FullTimeEmployee extends Employee
{
double monthlySalary;
FullTimeEmployee(String employeeName, double monthlySalary)
{
super(employeeName);
[Link] = monthlySalary;
}
@Override
void calculateSalary()
{
[Link]("Employee Name: " + employeeName);
[Link]("Employee Type: Full-Time");
[Link]("Monthly Salary: Rs. " + monthlySalary);
}
}
class PartTimeEmployee extends Employee
{
double hourlyRate;
int hoursWorked;
PartTimeEmployee(String employeeName, double hourlyRate, int hoursWorked)
{
super(employeeName);
[Link] = hourlyRate;
[Link] = hoursWorked;
}
@Override
void calculateSalary()
{
double salary = hourlyRate * hoursWorked;
[Link]("Employee Name: " + employeeName);
[Link]("Employee Type: Part-Time");
[Link]("Hourly Rate: Rs. " + hourlyRate);
[Link]("Hours Worked: " + hoursWorked);
[Link]("Salary: Rs. " + salary);
}
}
public class Main
{
public static void main(String[] args)
{
FullTimeEmployee f = new FullTimeEmployee("Ravi", 50000);
PartTimeEmployee p = new PartTimeEmployee("Kiran", 500, 80);
[Link]();
[Link]();
[Link]();
}
}
Output
Employee Name: Ravi
Employee Type: Full-Time
Monthly Salary: Rs. 50000.0
Employee Name: Kiran
Employee Type: Part-Time
Hourly Rate: Rs. 500.0
Hours Worked: 80
Salary: Rs. 40000.0
EXPERIMENT 2:
Title: Implementation of Multilevel Inheritance
Problem Statement
Design and implement a Java program to demonstrate multilevel inheritance using a banking
application. Create a base class Bank containing the bank name and a method to display bank details.
Derive a class Account from Bank containing account number and account holder name. Further derive a
class SavingsAccount from Account containing balance and a method to calculate interest. Create an
object of SavingsAccount and demonstrate how it can access the properties and methods of all three
levels of the inheritance hierarchy.
Implementation
class Bank
{
String bankName = "ABC Bank";
void displayBank()
{
[Link]("Bank Name: " + bankName);
}
}
class Account extends Bank
{
int accountNumber = 101;
String accountHolderName = "Ravi";
void displayAccount()
{
[Link]("Account Number: " + accountNumber);
[Link]("Account Holder Name: " + accountHolderName);
}
}
class SavingsAccount extends Account
{
double balance = 50000;
void calculateInterest()
{
double interest = balance * 0.05;
[Link]("Balance: " + balance);
[Link]("Interest: " + interest);
}
}
public class Main
{
public static void main(String[] args)
{
SavingsAccount obj = new SavingsAccount();
[Link]();
[Link]();
[Link]();
}
}
Output
Bank Name: ABC Bank
Account Number: 101
Account Holder Name: Ravi
Balance: 50000.0
Interest: 2500.0