OOP Basics
Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around “objects” rather than functions and logic. It models real-world entities by combining data (attributes) and the functions that operate on that data (methods) into a single unit called a class. This approach promotes modularity, reusability, and maintainability.
Benefits of OOP
Section titled “Benefits of OOP”OOP provides several advantages for software development:
- Modularity: Code is organized into self-contained classes, making it easier to manage.
- Reusability: Classes can be reused in different parts of a program or across multiple projects.
- Scalability: It simplifies the management of large and complex systems.
- Maintainability: Changes in one class are less likely to affect others.
- Encapsulation: Data and methods are bundled together, protecting data integrity.
- Inheritance: New classes can inherit properties, reducing code redundancy.
- Polymorphism: Objects can be treated as instances of their parent class, allowing for flexible code.
Procedural vs. Object-Oriented Programming
Section titled “Procedural vs. Object-Oriented Programming”| Feature | Procedural Oriented Programming (POP) | Object-Oriented Programming (OOP) |
|---|---|---|
| Approach | Top-down approach | Bottom-up approach |
| Structure | Program is divided into functions | Program is divided into objects |
| Data Access | Data is global and shared | Data is encapsulated within objects |
| Data Security | Less secure as data is exposed | More secure due to encapsulation |
| Focus | Focuses on functions (procedures) | Focuses on objects |
| Code Reusability | Less reusable | High reusability through inheritance |
| Inheritance | Does not support inheritance | Supports inheritance |
| Polymorphism | Does not support polymorphism | Supports polymorphism |
| Examples | C, Pascal | C++, Java, Python |
Classes & Objects
Section titled “Classes & Objects”At the core of OOP are classes and objects.
- A class is a blueprint or a template for creating objects. It defines the attributes (data) and methods (behavior) that all objects of that class will possess. Think of a class as the design for a car, specifying that it will have wheels and an engine.
- An object is a specific instance of a class. It represents a real-world entity with its own unique state (attributes) and behavior (methods). Following the car analogy, a red Toyota with 4 wheels is a specific object of the
Carclass.
Creating a Class and an Object
Section titled “Creating a Class and an Object”You can define a class using the class keyword.
# Defining an empty classclass Car: pass
# Defining a class with attributesclass Car: brand = "Toyota" # This is a class attribute price = 1000000In this example, brand and price are class attributes, which are shared among all instances of the Car class.
To create an object (or instantiate a class), you call the class as if it were a function.
my_car = Car() # Creating an objectprint(my_car.brand) # Output: ToyotaHere, my_car is an instance of the Car class. It can access the class attributes using dot notation. You can create multiple objects from the same class, and each will have its own identity and state.
Class Attributes vs. Instance Attributes
Section titled “Class Attributes vs. Instance Attributes”- Class Attributes are shared by all objects of a class. They are defined directly within the class body.
- Instance Attributes are unique to each object. They are assigned within a method using the
selfkeyword. If an instance and a class attribute have the same name, the instance attribute takes precedence for that specific object.
Constructors and self
Section titled “Constructors and self”A constructor is a special method used to initialize an object’s state when it is created. In Python, the constructor is the __init__ method.
__init__: This method is automatically called when an object is instantiated. It’s where you define and initialize instance attributes.self: This keyword refers to the instance of the class itself. It is the first parameter in any method of a class, including the constructor. It allows methods to access the object’s attributes and other methods.
Parameterized vs. Non-Parameterized Constructors
Section titled “Parameterized vs. Non-Parameterized Constructors”- A non-parameterized constructor has no arguments besides
self. It initializes attributes with fixed, predefined values.class Car:def __init__(self):self.brand = "Toyota" - A parameterized constructor takes additional arguments to initialize an object with values provided during instantiation.
class Car:def __init__(self, brand, color):self.brand = brandself.color = colormy_car = Car("Toyota", "Red")print(my_car.brand) # Output: Toyota