-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchoolManagement.java
More file actions
40 lines (35 loc) · 1.09 KB
/
Copy pathSchoolManagement.java
File metadata and controls
40 lines (35 loc) · 1.09 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
// Abstract superclass: Person
abstract class Person {
// Abstract method to be overridden by subclasses
abstract void performDuty();
}
// Subclass: Student
class Student extends Person {
@Override
void performDuty() {
System.out.println("Student is studying for exams.");
}
}
// Subclass: Teacher
class Teacher extends Person {
@Override
void performDuty() {
System.out.println("Teacher is conducting a lecture.");
}
}
// Main class to demonstrate runtime polymorphism
public class SchoolManagement {
public static void main(String[] args) {
// Creating an array of Person references
Person[] people = new Person[4];
// Filling the array with Student and Teacher instances
people[0] = new Student();
people[1] = new Teacher();
people[2] = new Student();
people[3] = new Teacher();
// Iterating through the array and calling performDuty()
for (Person p : people) {
p.performDuty(); // Runtime polymorphism in action
}
}
}