Employee Encapsulation and Method Overriding in Java π Project Overview This mini project demonstrates the importance of Encapsulation in Java and shows how method overriding works in an inheritance hierarchy. It compares two approaches to handling employee data: Without Encapsulation β where class fields are openly accessible and can be modified directly. With Encapsulation β where sensitive data is protected using private variables and controlled access through getters and setters. The project also includes a logic-based method to evaluate employee performance based on salary and bonus. π§ Concepts Covered: Encapsulation (data hiding) Inheritance Method Overriding Access Modifiers Basic conditional logic
Object-Oriented Programming (OOP) principles in Java:
ποΈ Project Structure EmployeeEncapsulation/ β βββ Employee.java // Non-encapsulated class βββ SecureEmployee.java // Encapsulated subclass with method overriding βββ Main.java // Driver class with main() method
π§© Class Description 1οΈβ£ Employee (Non-Encapsulated Class)
Fields (default access): String name double salary
Method: displayDetails() Displays basic employee information.
β‘οΈ Fields can be modified directly, showing lack of data protection.
2οΈβ£ SecureEmployee (Encapsulated Subclass)
Private Field: double bonus Public Methods: Getter and Setter for bonus Overridden displayDetails() to include bonus
performanceStatus() method with logic:
Salary + Bonus β₯ 100000 β Excellent Performer
Salary + Bonus β₯ 50000 β Good Performer
Else β Needs Improvement
β‘οΈ Demonstrates proper encapsulation and controlled data access.
β¨οΈ Input Format EmployeeName Salary Bonus
Example Input Riya 45000 10000
π€ Output Format Without Encapsulation: <displayDetails() output>
With Encapsulation (Overridden): <displayDetails() output>
Performance Status: <performanceStatus() output>
Example Output Without Encapsulation: Name : Riya Salary : 45000.0 (Fields can be modified directly!)
With Encapsulation (Overridden): Name : Riya Salary : 45000.0 Bonus : 10000.0
Performance Status: Good Performer
π― Key Learning Outcome Understand why encapsulation is essential for data security. See how method overriding allows subclasses to extend or modify behavior. Learn how logic-based methods improve program functionality. Gain practical exposure to real-world OOP design principles. π How to Run the Project Clone the repository Open the project in any Java IDE (Eclipse / IntelliJ / VS Code) Compile and run the Main.java file Provide input as specified
π Suitable For Java beginners OOP concept practice College mini projects Interview preparation
π€ Author
Durgesh Tiwari Java Full Stack Trainee Naresh IT,Hyderabad