Core OOP Principles
1. Inheritance and Polymorphism: University Login System
In real-world systems, different user types (students, instructors, administrators) share common features but require different security levels. This exercise demonstrates inheritance (code reuse through parent-child class relationships) and polymorphism (same method call, different behavior) using password generation and validation.
Class Requirements
User Class (Parent Class)
Variables: username, password, email (all String)
Methods:
- Constructors (default and parameterized)
- Getters/setters for all variables
generatePassword()- ReturnsBasic password requirements: 6+ charactersvalidatePassword(String password)- Returns true iflength >= 6getAccessLevel()- ReturnsBasic Userlogin(String enteredPassword)- Returns true ifpasswordmatchesdisplayUserInfo()- Prints all user details
Student Class (extends User)
Additional Variables: studentID, major (both String)
Constructor: Use super() to call parent constructor, then initialize studentID and major
Override these methods:
generatePassword()- "Student:6+characters,1number required"validatePassword(String)- Check:length >= 6AND has at least1digitgetAccessLevel()-Student AccessdisplayUserInfo()- Callsuper.displayUserInfo(), then printstudentIDandmajor
Instructor Class (extends User)
Additional Variables: employeeID, department (both String, private)
Constructor: Use super(), then initialize employeeID and department
Override these methods:
generatePassword()- "Instructor:8+characters, uppercase, lowercase,1number"validatePassword(String)- Check:length >= 8 AND has uppercase AND lowercase AND digitgetAccessLevel()-Instructor AccessdisplayUserInfo()- Callsuper.displayUserInfo(), then printemployeeIDand department
Administrator Class (extends User)
Additional Variables: adminID, securityClearance (both String, private)
Constructor: Use super(), then initialize adminID and securityClearance
Override these methods:
generatePassword()- "Admin:10+characters, uppercase, lowercase, number, special char (!@#$%^&)validatePassword(String)- Check:length >= 10 AND uppercase AND lowercase AND digit AND special chargetAccessLevel()-Administrator Access - Full ControldisplayUserInfo()- Callsuper.displayUserInfo(), then printadminIDand clearance
Main Class Requirements
- Create three users (
Student,Instructor,Administrator). - Store all three in a
User[]array - Loop through array and for each user:
- Call
displayUserInfo() - Call
generatePassword()and print result - Call
getAccessLevel()and print result - Test password validation with different passwords:
- Weak: "
pass123" - Moderate: "
Pass123" - Strong: "
Pass123!@#" - Set valid passwords and test
login()method
Implementation Tips
- Use
@Overrideannotation when overriding methods - Check character types:
Character.isUpperCase(),Character.isLowerCase(),Character.isDigit() - Check special chars:
"!@#$%^&".indexOf(password.charAt(i)) >= 0 - Loop through password with:
for (int i = 0; i < password.length(); i++)