-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInheritance4.java
170 lines (146 loc) · 5.18 KB
/
Inheritance4.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//EXPERIMENT 4
/* Write a Java program which creates a class named 'Employee' having the following members: Name, Age, Phone number, Address, Salary.
It also has a method named 'printSalary( )' which prints the salary of the Employee.
Two classes 'Officer' and 'Manager' inherits the 'Employee' class.
The 'Officer' and 'Manager' classes have data members 'specialization' and 'department' respectively.
Now, assign name, age, phone number, address and salary to an officer and a manager by making an object of both of these classes and
print the same. (Exercise to understand inheritance). */
import java.util.Scanner;
class Employee {
// Member variables
String name;
int age;
String phoneNumber;
String address;
double salary;
// Constructor
Employee(String name, int age, String phoneNumber, String address, double salary) {
this.name = name;
this.age = age;
this.phoneNumber = phoneNumber;
this.address = address;
this.salary = salary;
}
// Method to print the salary
void printSalary() {
System.out.println("Salary: $" + salary);
}
// Method to print general details
void printDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Phone Number: " + phoneNumber);
System.out.println("Address: " + address);
}
}
// Derived class Officer
class Officer extends Employee {
// Additional member variable
String specialization;
// Constructor
Officer(String name, int age, String phoneNumber, String address, double salary, String specialization) {
super(name, age, phoneNumber, address, salary);
this.specialization = specialization;
}
// Method to print details specific to Officer
void printOfficerDetails() {
printDetails();
System.out.println("Specialization: " + specialization);
printSalary();
}
}
// Derived class Manager
class Manager extends Employee {
// Additional member variable
String department;
// Constructor
Manager(String name, int age, String phoneNumber, String address, double salary, String department) {
super(name, age, phoneNumber, address, salary);
this.department = department;
}
// Method to print details specific to Manager
void printManagerDetails() {
printDetails();
System.out.println("Department: " + department);
printSalary();
}
}
// Main class to test the implementation
public class Employee4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input for Officer
System.out.println("Enter details for Officer:");
System.out.print("Name: ");
String officerName = scanner.nextLine();
System.out.print("Age: ");
int officerAge = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Phone Number: ");
String officerPhoneNumber = scanner.nextLine();
System.out.print("Address: ");
String officerAddress = scanner.nextLine();
System.out.print("Salary: ");
double officerSalary = scanner.nextDouble();
scanner.nextLine(); // Consume newline
System.out.print("Specialization: ");
String officerSpecialization = scanner.nextLine();
Officer officer = new Officer(officerName, officerAge, officerPhoneNumber, officerAddress, officerSalary, officerSpecialization);
// Input for Manager
System.out.println("\nEnter details for Manager:");
System.out.print("Name: ");
String managerName = scanner.nextLine();
System.out.print("Age: ");
int managerAge = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Phone Number: ");
String managerPhoneNumber = scanner.nextLine();
System.out.print("Address: ");
String managerAddress = scanner.nextLine();
System.out.print("Salary: ");
double managerSalary = scanner.nextDouble();
scanner.nextLine(); // Consume newline
System.out.print("Department: ");
String managerDepartment = scanner.nextLine();
Manager manager = new Manager(managerName, managerAge, managerPhoneNumber, managerAddress, managerSalary,
managerDepartment);
// Print details
System.out.println("\nOfficer Details:");
officer.printOfficerDetails();
System.out.println("\nManager Details:");
manager.printManagerDetails();
// Close the scanner
scanner.close();
}
}
//SAMPLE OUTPUT
/*
Enter details for Officer:
Name: John Doe
Age: 35
Phone Number: 123-456-7890
Address: 123 Maple Street
Salary: 50000
Specialization: Software Engineering
Enter details for Manager:
Name: Jane Smith
Age: 40
Phone Number: 987-654-3210
Address: 456 Oak Avenue
Salary: 75000
Department: Human Resources
Officer Details:
Name: John Doe
Age: 35
Phone Number: 123-456-7890
Address: 123 Maple Street
Specialization: Software Engineering
Salary: $50000.0
Manager Details:
Name: Jane Smith
Age: 40
Phone Number: 987-654-3210
Address: 456 Oak Avenue
Department: Human Resources
Salary: $75000.0
*/