Oop Ca1
Oop Ca1
Oop Ca1
• Objects: In OOP, we design computer programs by creating and interacting with objects. Each
object represents a specific entity or concept. For example, if you’re building a banking
application, you might have objects like “Account,” “Customer,” and “Transaction.”
• Code (Methods): Objects also contain code (methods) that define their behavior. Methods allow
objects to perform actions or respond to specific events. For example, an “Account” object
could have methods like “deposit,” “withdraw,” and “getBalance.”
An object is an instance of a class. It represents a specific entity or concept in the real world. For example, if
you have a “Car” class, an object of that class could be a specific car with its unique characteristics (color,
make, model, etc.).
2. Classes:
A class is a blueprint or template for creating objects. It defines the attributes (data) and methods (functions)
that objects of that class will have. Think of it as the general description of what an object should look like.
3. Attributes (Fields/Properties):
Attributes (also called fields or properties) are the data members of a class. They represent the state of an
object. For instance, a “Person” class might have attributes like “name,” “age,” and “address.”
4. Methods (Functions):
Methods define the behavior of objects. They are functions associated with a class. For example, a
“BankAccount” class could have methods like “deposit,” “withdraw,” and “getBalance.”
• Remember, these concepts form the foundation of OOP, allowing developers to create well-organized,
reusable, and maintainable code.
Pillars of OOP
1. ENCAPSULATION:
Definition: Encapsulation involves bundling data (attributes) and methods (behavior) together within a
class. It hides internal complexities and exposes only necessary interfaces.
Purpose: Encapsulation ensures that data is accessed and modified through well-defined methods
(getters and setters), preventing direct manipulation and maintaining dataintegrity.
Example: Think of a Bird class with properties like color and legs. Encapsulation allows us to control how
these attributes are accessed and modified1.
2. INHERITANCE:
Definition: Inheritance allows a class (called a subclass or derived class) to inherit attributes and methods
from another class (called a superclass or base class). It promotes code reuse.
Purpose: By inheriting common features (like wings, eyes, etc.), we avoid redundant declarations. For
instance, a Pigeon class can inherit from the general Bird class1.
3. POLYMORPHISM:
Purpose : Polymorphism enhances flexibility. For example, both a Circle and a Rectangle
class can have a calculateArea method, each behaving differently1.
4. ABSTRACTION:
Purpose : Abstraction allows us to create generic functions or objects that serve multiple
purposes. For instance, abstracting away coffee-making details into a simple button
interface1.
• Remember, these pillars form the foundation of OOP, enabling better code
organization, reusability, and maintainability.
OOP Design Principles
❑ Single-Responsibility Principle (SRP):
• A class should have one and only one reason to change. In other words, it
should have a single responsibility.
• In practice, this means you can add new functionality (extensions) without
altering existing code. Achieve this through interfaces, abstract classes, and
inheritance1.
❑ Liskov Substitution Principle (LSP):
• Clients (classes or modules) should not be forced to depend on interfaces they don’t
use.
• Split large interfaces into smaller, more specific ones. This prevents unnecessary
dependencies and promotes cleaner designs1.
• High-level modules should not depend on low-level modules. Both should depend on
abstractions (interfaces or abstract classes).