Name – Paulam Das
UID – TNU2022053100004
Dept. – C.S.E. (AI & ML)
Sem – IV
Experiment-4
Q. Write a Multi-level Inheritance program that contains 3 no-arg constructors
in each class. Observe the firing sequence of constructors.
Code:
class Gp{
Gp(){
[Link]("This is the Grandparent class");
}
}
class P extends Gp{
P(){
[Link]("This is the parent class");
}
}
class C extends P{
C(){
[Link]("This is a child class");
}
}
class Prog{
public static void main(String args[]){
C t1 = new C();
}
}
Output:
Q. What steps can be taken to over-come method overriding?
Demonstrate the same.
Code:
class GParent{
int res=1;
void compute(){
[Link]("Result = "+res);
}
}
class Parent extends GParent{
Parent(){
[Link]();
}
void compute(){
res=2;
[Link]("Result = "+res);
}
}
class Child extends Parent{
Child(){
[Link]();
}
void compute(){
res = 3;
[Link]("Result = "+res);
}
}
class Prog{
public static void main(String args[]){
Child c1 = new Child();
[Link]();
}
}
Output:
Q. Case Study: Employee Management System.
You are tasked with developing an Employee Management System for a
company. The system should have the following requirements:
1. There are two types of employees:
a. Full-time employees
b. Part-time employees.
2. Both types of employees have basic information such as name,
employee ID, and salary.
3. Full-time employees have an additional attribute - annual salary.
4. Part-time employees have an additional attribute - hourly rate and
number of hours worked.
All employees should be able to calculate their monthly earnings.
Your task is to design and implement a Java program to fulfill these
requirements using inheritance and method overriding.
Design and implement the classes Employee, FullTimeEmployee, and
PartTimeEmployee. Make sure to properly use inheritance and method
overriding to achieve the desired functionality.
Your implementation should include:
● A superclass Employee with attributes for name, employee ID, and
salary. Include a method calculateMonthlyEarnings() that returns the
monthly earnings.
● Two subclasses FullTimeEmployee and PartTimeEmployee inheriting
from Employee. Each subclass should include additional attributes and
appropriate methods to calculate monthly earnings.
Code:
import [Link];
class Employee{
Scanner sc = new Scanner([Link]);
double salary;
String eid,name;
void calculateMonthlyEarnings(){
[Link]("\nEnter your name: ");
name = [Link]();
[Link]("Enter Employee ID: ");
eid = [Link]();
}
}
class FullTimeEmployee extends Employee{
double annual;
FullTimeEmployee(){
[Link]();
}
void calculateMonthlyEarnings(){
[Link]("Enter your annual salary: ");
annual = [Link]();
salary = annual/12;
[Link]("----------Full-Time Employee Management
Screen----------");
[Link]("Name: "+name);
[Link]("Employee ID: "+eid);
[Link]("Monthly Salary: "+salary);
}
}
class PartTimeEmployee extends Employee{
double hrate;
int hours;
PartTimeEmployee(){
[Link]();
}
void calculateMonthlyEarnings(){
[Link]("Enter your hourly rate: ");
hrate = [Link]();
[Link]("Enter hours worked in a day: ");
hours = [Link]();
salary = (hrate*hours)*30;
[Link]("----------Part-Time Employee Management
Screen----------");
[Link]("Name: "+name);
[Link]("Employee ID: "+eid);
[Link]("Monthly Salary: "+salary);
}
}
class Prog{
public static void main(String args[]){
FullTimeEmployee fe = new FullTimeEmployee();
[Link]();
PartTimeEmployee pe = new PartTimeEmployee();
[Link]();
}
}
Output: