-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritance.java
More file actions
70 lines (65 loc) · 1.7 KB
/
Inheritance.java
File metadata and controls
70 lines (65 loc) · 1.7 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
/**************************************************************
* File : Inheritence.java
* Description : Exercise to understand Inheritance.
* Author : Amal Joy
* Date : 13-10-2023
***************************************************************/
import java.util.Scanner;
public class Inheritance {
public static void main(String [] args) {
Officer officer=new Officer();
Manager manager=new Manager();
System.out.println("Enter the officer details:");
officer.getDetails();
officer.getSpec();
officer.printDetails();
officer.printSpec();
System.out.println("Enter the manager details:");
manager.getDetails();
manager.getDept();
manager.printDetails();
manager.printDept();
}
}
class Employee{
String name;
int age;
long phNo;
String address;
int salary;
Scanner sc=new Scanner(System.in);
public void getDetails() {
System.out.println("Enter name,age,PhoneNo.,address,salary:");
name=sc.nextLine();
age=sc.nextInt();
phNo=sc.nextLong();
sc.nextLine();
address=sc.nextLine();
salary=sc.nextInt();
}
public void printDetails() {
System.out.printf("Name=%s\nAge=%d\nPhoneNo.=%d\nAddress=%s\nSalary=%d\n",name,age,phNo,address,salary);
}
}
class Officer extends Employee{
String specialization;
public void getSpec() {
System.out.println("Enter specialization:");
sc.nextLine();
specialization=sc.nextLine();
}
public void printSpec() {
System.out.println("Specialization="+specialization);
}
}
class Manager extends Employee{
String department;
public void getDept() {
System.out.println("Enter Department:");
sc.nextLine();
department=sc.nextLine();
}
public void printDept() {
System.out.println("Department="+department);
}
}