OOPS Syllabus:
Encapsulation is the process of bundling data (fields) and methods that operate
on the data into a single unit (class). It also involves restricting direct access to
some of the object’s components to maintain control and prevent unintended
interference or misuse.
Key Concepts:
Access Modifiers: Control the visibility of class members.
o Public: Accessible from any other code.
o Private: Accessible only within the defining class.
Benefits:
1. Protects the internal state of an object.
2. Promotes modularity by allowing you to expose only necessary details.
3. Enhances maintainability by reducing the impact of changes.
Encapsulation in Action:
The balance field is private, preventing direct modification.
Controlled access is provided via Deposit methods.
Abstraction in Simple Terms
Definition:
Interface:
Key Concepts
1. Single Inheritance
Multiple Inheritance
FlyingCar myFlyingCar = new FlyingCar();
[Link]();[Link]();
1. Polymorphism Definition
Compile-time polymorphism: It is resolved during compile time.
Method Overriding
Method overriding occurs when a derived class provides a specific
implementation for a method already defined in the base class.
Runtime polymorphism: It is resolved during runtime.
Achieved using the virtual and override keywords in C#.
Animal animal = new Dog();
[Link](); // Output: Dog barks
SOLID PRINCIPLES:
The SOLID principles are five design principles that help developers create
software that is modular, scalable, and easy to maintain. These principles were
popularized by Robert C. Martin and are essential concepts in object-oriented
programming (OOP). They are often used to evaluate the quality of a system's
design and ensure that each class or module in an application has a single,
well-defined responsibility.
1. Single Responsibility Principle (SRP)
Definition: A class should have only one reason to change, meaning it should
have a single responsibility or purpose.
Explanation: In SRP, each class should focus on a single job or responsibility.
This keeps classes small, reduces complexity, and makes the code easier to test
and maintain. If a class has multiple responsibilities, changes in one
responsibility may affect the other, leading to bugs or unexpected behavior.
2. Open/Closed Principle (OCP)
Definition: Software entities (classes, modules, functions) should be open for
extension but closed for modification.
Explanation: OCP encourages designing classes so that their behavior can be
extended without modifying existing code. This reduces the risk of introducing
new bugs when requirements change and promotes reusability. Typically, this is
achieved through abstraction and inheritance.
Example: Imagine a DiscountCalculator class that calculates different
types of discounts. If you add each discount type directly to this class, you’ll
need to modify it whenever a new discount type is introduced, violating OCP.
3. Liskov Substitution Principle (LSP)
Definition: Derived classes should be substitutable for their base classes without
affecting the correctness of the program.
Example: Suppose we have a Bird class and a derived Penguin class. Since
Penguin cannot fly, adding a Fly method in Bird could violate LSP.
4. Interface Segregation Principle (ISP)
Definition: Clients should not be forced to implement interfaces they don’t use.
Split large interfaces into smaller, specific ones.
5. Dependency Inversion Principle (DIP)
The Dependency Inversion Principle (DIP) is all about reducing dependency on
specific implementations so that your code is more flexible, easier to maintain,
and adaptable to change.
Real-World Example
Imagine you have a car (high-level module) that needs a battery (low-level
module). If the car is "hardcoded" to use only one type of battery, then every
time you want to change the battery type, you'd have to modify the car itself—
this would be inconvenient and rigid.
With DIP, you would create an interface called IBattery that defines the
essential functions of a battery. Now, the car depends on IBattery, not on
any specific battery brand or type. This means you can plug in any battery that
follows the IBattery interface, and the car will still work without
modification.