Repository navigation
Expand file tree
/
Copy pathinheritanceExample4Employee.java
More file actions
98 lines (84 loc) · 2.75 KB
/
Copy pathinheritanceExample4Employee.java
File metadata and controls
98 lines (84 loc) · 2.75 KB
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
package OOPSC26CS070;
import java.util.Scanner;
class Employee1 {
String name, address;
int age, mob;
float sal;
void printEmployee() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Address: " + address);
System.out.println("Phone Number: " + mob);
}
// Method to print salary
void printSalary() {
System.out.print(sal);
}
}
class Officer extends Employee1 {
String specialization;
// Method to print specialization
void printSpecialization() {
System.out.print(specialization);
}
}
class Manager extends Employee1 {
String department;
// Method to print department
void printDepartment() {
System.out.print(department);
}
}
public class inheritanceExample4Employee {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Create Officer object
Officer off = new Officer();
System.out.println("Enter the officer's name:");
off.name = sc.nextLine();
System.out.println("Enter Address:");
off.address = sc.nextLine();
System.out.println("Enter Specialization:");
off.specialization = sc.nextLine();
System.out.println("Enter Age:");
off.age = sc.nextInt();
System.out.println("Enter Phone Number:");
off.mob = sc.nextInt();
System.out.println("Enter Salary:");
off.sal = sc.nextFloat();
sc.nextLine(); // Consume the leftover newline
// Create Manager object
Manager man = new Manager();
System.out.println("Enter the manager's name:");
man.name = sc.nextLine();
System.out.println("Enter Address:");
man.address = sc.nextLine();
System.out.println("Enter Department:");
man.department = sc.nextLine();
System.out.println("Enter Age:");
man.age = sc.nextInt();
System.out.println("Enter Phone Number:");
man.mob = sc.nextInt();
System.out.println("Enter Salary:");
man.sal = sc.nextFloat();
// Display details
System.out.println("OFFICER DETAILS:");
off.printEmployee();
//System.out.println();
System.out.print("Salary: ");
off.printSalary();
System.out.println();
System.out.print("Specialization: ");
off.printSpecialization();
System.out.println();
System.out.println();
System.out.println("MANAGER'S DETAILS:");
man.printEmployee();
System.out.print("Salary: ");
man.printSalary();
System.out.println();
System.out.print("Department: ");
man.printDepartment();
System.out.println();
}
}