OOP Principles

1. Encapsulation Meaning: It’s about bundling the data (attributes) and the methods (functions) that operate on the data into a single unit called an object. Moreover, restricting direct access to some of an object’s components, which is a means to prevent unintended interference and misuse of data. Java Example: Using private modifiers for class attributes and providing public getters and setters to access and modify them. public class Person { private String name; public String getName() { return name; } public void setName(String name) { this....

October 17, 2023

SOLID Principles

1. Single Responsibility Principle (SRP) Meaning: A class should have only one reason to change, meaning it should have only one job or responsibility. Java Example: Instead of a single class handling both data storage and data representation, you split these tasks into separate classes. // Combining responsibilities (not ideal) public class User { public void saveUserToDatabase() {/*...*/} public void displayUserDetails() {/*...*/} } // Split responsibilities (preferred) public class UserDatabase { public void saveUser(User user) {/*....

October 17, 2023