[go: up one dir, main page]

0% found this document useful (0 votes)
4 views7 pages

Code

Uploaded by

Zahid ullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Code

Uploaded by

Zahid ullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Object-Oriented Design: Inheritance and

Polymorphism
To demonstrate the core object-oriented programming (OOP) principles within the
Face Recognition Attendance System, we will focus on the User hierarchy, showcasing
inheritance and polymorphism. This design approach promotes code reusability,
maintainability, and extensibility, which are crucial for a robust and scalable system.
3 .1 Inheritance: The User Hierarchy
Inheritance allows us to define a general User class with common attributes and
behaviors, and then create more specialized classes (e.g., Student , Instructor ,
Admin ) that inherit these common characteristics while adding their own unique
properties and methods. This models the 'is-a' relationship (e.g., a Student is a
User ).
Base Class: User
The User class serves as the foundation for all types of users in the system. It
encapsulates common attributes such as userId , name , and email . It also provides a
generic displayDetails() method that can be overridden by subclasses to provide
more specific information, along with common actions like updateProfile() and
resetPassword() .class User {
private String userId;
private String name;
private String email;
public User(String userId, String name, String email) {
this.userId = userId;
this.name = name;
this.email = email;
}
public String getUserId() {
return userId;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public void displayDetails() {
System.out.println("User ID: " + userId);
System.out.println("Name: " + name);
System.out.println("Email: " + email);
}
public void updateProfile() {
System.out.println(getName() + " is updating their profile.");
}
public void resetPassword() {
System.out.println(getName() + " is resetting their password.");
}
}
Derived Class: Student
The Student class extends User , inheriting its properties and methods. It adds
specific attributes like studentId and major , and introduces methods specific to
student
functionalities
such
as
enrollFaceData() ,
recognizeFace() ,
viewAttendance() ,
viewSchedule() ,
viewGrades() ,
and
receiveNotifications() .class Student extends User {
private String studentId;
private String major;
public Student(String userId, String name, String email, String studentId,
String major) {
super(userId, name, email);
this.studentId = studentId;
this.major = major;
}
public String getStudentId() {
return studentId;
}
public String getMajor() {
return major;
}
@Override
public void displayDetails() {
super.displayDetails();
System.out.println("Student ID: " + studentId);
System.out.println("Major: " + major);
}
public void enrollFaceData() {
System.out.println(getName() + " (Student ID: " + studentId + ") is
enrolling face data.");
}
public void recognizeFace() {
System.out.println(getName() + " (Student ID: " + studentId + ") is
attempting face recognition for attendance.");
}
public void viewAttendance() {
System.out.println(getName() + " (Student ID: " + studentId + ") is
viewing attendance records.");
}
public void viewSchedule() {
System.out.println(getName() + " (Student ID: " + studentId + ") is
viewing their schedule.");
}
public void viewGrades() {
System.out.println(getName() + " (Student ID: " + studentId + ") is
viewing their grades.");
}
public void receiveNotifications() {
System.out.println(getName() + " (Student ID: " + studentId + ")
received a notification.");
}
}
Derived Class: InstructorSimilarly, the
Instructor class extends
User , adding instructorId and
department . It includes methods reflecting an instructor's specific responsibilities,
such
as
initiateFaceRecognition() ,
viewAttendanceReports() ,
manageCourses() ,
manageStudents() ,
generateReports() ,
configureRecognitionSettings() , exportData() , and receiveNotifications() .class Instructor extends
User {
private String instructorId;
private String department;
public Instructor(String userId, String name, String email, String
instructorId, String department) {
super(userId, name, email);
this.instructorId = instructorId;
this.department = department;
}
public String getInstructorId() {
return instructorId;
}
public String getDepartment() {
return department;
}
@Override
public void displayDetails() {
super.displayDetails();
System.out.println("Instructor ID: " + instructorId);
System.out.println("Department: " + department);
}
public void initiateFaceRecognition() {
System.out.println(getName() + " (Instructor ID: " + instructorId + ")
is initiating face recognition for the class.");
}
public void viewAttendanceReports() {
System.out.println(getName() + " (Instructor ID: " + instructorId + ")
is viewing attendance reports.");
}
public void manageCourses() {
System.out.println(getName() + " (Instructor ID: " + instructorId + ")
is managing courses.");
}
public void manageStudents() {
System.out.println(getName() + " (Instructor ID: " + instructorId + ")
is managing students.");
}
public void generateReports() {
System.out.println(getName() + " (Instructor ID: " + instructorId + ")
is generating reports.");
}
public void configureRecognitionSettings() {
System.out.println(getName() + " (Instructor ID: " + instructorId + ")
is configuring recognition settings.");
}
public void exportData() {
System.out.println(getName() + " (Instructor ID: " + instructorId + ")
is exporting data.");
}public void receiveNotifications() {
System.out.println(getName() + " (Instructor ID: " + instructorId + ")
received a notification.");
}
}
Derived Class: Admin
The Admin class also extends User , with adminId and role . It features methods
specific to administrative tasks, such as manageUsers() , manageCourses() ,
viewSystemLogs() ,
generateSystemWideReports() ,
manageFaceTemplates() ,
configureRecognitionSettings() ,
exportData() ,
and
configureSystemSettings() .class Admin extends User {
private String adminId;
private String role;
public Admin(String userId, String name, String email, String adminId,
String role) {
super(userId, name, email);
this.adminId = adminId;
this.role = role;
}
public String getAdminId() {
return adminId;
}
public String getRole() {
return role;
}
@Override
public void displayDetails() {
super.displayDetails();
System.out.println("Admin ID: " + adminId);
System.out.println("Role: " + role);
}
public void manageUsers() {
System.out.println(getName() + " (Admin ID: " + adminId + ") is
managing users.");
}
public void manageCourses() {
System.out.println(getName() + " (Admin ID: " + adminId + ") is
managing courses.");
}
public void viewSystemLogs() {
System.out.println(getName() + " (Admin ID: " + adminId + ") is viewing
system logs.");
}
public void generateSystemWideReports() {
System.out.println(getName() + " (Admin ID: " + adminId + ") is
generating system-wide reports.");
}
public void manageFaceTemplates() {
System.out.println(getName() + " (Admin ID: " + adminId + ") is
managing face templates.");
}
public void configureRecognitionSettings() {
System.out.println(getName() + " (Admin ID: " + adminId + ") is
configuring system-wide recognition settings.");
}
public void exportData() {
System.out.println(getName() + " (Admin ID: " + adminId + ") is
exporting system data.");
}public void configureSystemSettings() {
System.out.println(getName() + " (Admin ID: " + adminId + ") is
configuring system settings.");
}
}
3 .2 Polymorphism: Flexible User Interactions
Polymorphism, meaning "many forms," allows objects of different classes to be
treated as objects of a common superclass. This is particularly powerful when dealing
with collections of diverse objects, enabling uniform handling while still allowing
specific behaviors to be invoked. In our system, polymorphism is demonstrated by
treating Student , Instructor , and Admin objects as generic User objects. When
common methods like displayDetails() , updateProfile() , or resetPassword()
are called on a User reference, the appropriate overridden method in the actual
object's class is executed, showcasing runtime polymorphism. This allows for a flexible
and extensible system where new user types can be added without modifying existing
code that interacts with the User base class.public class Main {
public static void main(String[] args) {
System.out.println("--- User Details ---");
User user1 = new User("U001", "Generic User", "user@example.com");
user1.displayDetails();
user1.updateProfile();
user1.resetPassword();
System.out.println();
System.out.println("--- Student Details ---");
Student student1 = new Student("U002", "Ali Khan",
"ali.khan@university.edu", "S1001", "Computer Science");
student1.displayDetails();
student1.enrollFaceData();
student1.recognizeFace();
student1.viewAttendance();
student1.viewSchedule();
student1.viewGrades();
student1.receiveNotifications();
student1.updateProfile();
student1.resetPassword();
System.out.println();
System.out.println("--- Instructor Details ---");
Instructor instructor1 = new Instructor("U003", "Dr. Fatima Ahmed",
"fatima.ahmed@university.edu", "I2001", "Software Engineering");
instructor1.displayDetails();
instructor1.initiateFaceRecognition();
instructor1.viewAttendanceReports();
instructor1.manageCourses();
instructor1.manageStudents();
instructor1.generateReports();
instructor1.configureRecognitionSettings();
instructor1.exportData();
instructor1.receiveNotifications();
instructor1.updateProfile();
instructor1.resetPassword();
System.out.println();
System.out.println("--- Admin Details ---");
Admin admin1 = new Admin("U004", "Mr. Hassan Raza",
"hassan.raza@university.edu", "A3001", "System Administrator");
admin1.displayDetails();
admin1.manageUsers();
admin1.manageCourses();
admin1.viewSystemLogs();
admin1.generateSystemWideReports();
admin1.manageFaceTemplates();
admin1.configureRecognitionSettings();
admin1.exportData();
admin1.configureSystemSettings();
admin1.updateProfile();
admin1.resetPassword();
System.out.println();
System.out.println("--- Polymorphism Example (Array of Users) ---");
User[] users = new User[3];
users[0] = student1;
users[1] = instructor1;
users[2] = admin1;for (User user : users) {
user.displayDetails(); // Polymorphic call
user.updateProfile(); // Polymorphic call
user.resetPassword(); // Polymorphic call
if (user instanceof Student) {
((Student) user).enrollFaceData();
((Student) user).recognizeFace();
} else if (user instanceof Instructor) {
((Instructor) user).initiateFaceRecognition();
((Instructor) user).viewAttendanceReports();
} else if (user instanceof Admin) {
((Admin) user).manageUsers();
((Admin) user).manageFaceTemplates();
}
System.out.println();
}
}
}
Output of the Java Code:--- User Details ---
User ID: U001
Name: Generic User
Email: user@example.com
Generic User is updating their profile.
Generic User is resetting their password.
--- Student Details ---
User ID: U002
Name: Ali Khan
Email: ali.khan@university.edu
Student ID: S1001
Major: Computer Science
Ali Khan (Student ID: S1001) is enrolling face data.
Ali Khan (Student ID: S1001) is attempting face recognition for attendance.
Ali Khan (Student ID: S1001) is viewing attendance records.
Ali Khan (Student ID: S1001) is viewing their schedule.
Ali Khan (Student ID: S1001) is viewing their grades.
Ali Khan (Student ID: S1001) received a notification.
Ali Khan is updating their profile.
Ali Khan is resetting their password.
--- Instructor Details ---
User ID: U003
Name: Dr. Fatima Ahmed
Email: fatima.ahmed@university.edu
Instructor ID: I2001
Department: Software Engineering
Dr. Fatima Ahmed (Instructor ID: I2001) is initiating face recognition for the
class.
Dr. Fatima Ahmed (Instructor ID: I2001) is viewing attendance reports.
Dr. Fatima Ahmed (Instructor ID: I2001) is managing courses.
Dr. Fatima Ahmed (Instructor ID: I2001) is managing students.
Dr. Fatima Ahmed (Instructor ID: I2001) is generating reports.
Dr. Fatima Ahmed (Instructor ID: I2001) is configuring recognition settings.
Dr. Fatima Ahmed (Instructor ID: I2001) is exporting data.
Dr. Fatima Ahmed (Instructor ID: I2001) received a notification.
Dr. Fatima Ahmed is updating their profile.
Dr. Fatima Ahmed is resetting their password.
--- Admin Details ---
User ID: U004
Name: Mr. Hassan Raza
Email: hassan.raza@university.edu
Admin ID: A3001
Role: System Administrator
Mr. Hassan Raza (Admin ID: A3001) is managing users.
Mr. Hassan Raza (Admin ID: A3001) is managing courses.
Mr. Hassan Raza (Admin ID: A3001) is viewing system logs.
Mr. Hassan Raza (Admin ID: A3001) is generating system-wide reports.
Mr. Hassan Raza (Admin ID: A3001) is managing face templates.
Mr. Hassan Raza (Admin ID: A3001) is configuring system-wide recognition
settings.
Mr. Hassan Raza (Admin ID: A3001) is exporting system data.
Mr. Hassan Raza (Admin ID: A3001) is configuring system settings.
Mr. Hassan Raza is updating their profile.
Mr. Hassan Raza is resetting their password.
--- Polymorphism Example (Array of Users) ---
User ID: U002
Name: Ali KhanEmail: ali.khan@university.edu
Student ID: S1001
Major: Computer Science
Ali Khan is updating their profile.
Ali Khan is resetting their password.
Ali Khan (Student ID: S1001) is enrolling face data.
Ali Khan (Student ID: S1001) is attempting face recognition for attendance.
User ID: U003
Name: Dr. Fatima Ahmed
Email: fatima.ahmed@university.edu
Instructor ID: I2001
Department: Software Engineering
Dr. Fatima Ahmed is updating their profile.
Dr. Fatima Ahmed is resetting their password.
Dr. Fatima Ahmed (Instructor ID: I2001) is initiating face recognition for the
class.
Dr. Fatima Ahmed (Instructor ID: I2001) is viewing attendance reports.
User ID: U004
Name: Mr. Hassan Raza
Email: hassan.raza@university.edu
Admin ID: A3001
Role: System Administrator
Mr. Hassan Raza is updating their profile.
Mr. Hassan Raza is resetting their password.
Mr. Hassan Raza (Admin ID: A3001) is managing users.
Mr. Hassan Raza (Admin ID: A3001) is managing face templates.
This output clearly demonstrates how the displayDetails() , updateProfile() , and
resetPassword() methods behave differently for each subclass, even when called
through a User reference, illustrating polymorphism in action. The instanceof
checks further show how specific methods unique to each subclass can be invoked
after downcasting, allowing for specialized behavior based on the actual object type.

You might also like