Skip to content

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.

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”
FeatureProcedural Oriented Programming (POP)Object-Oriented Programming (OOP)
ApproachTop-down approachBottom-up approach
StructureProgram is divided into functionsProgram is divided into objects
Data AccessData is global and sharedData is encapsulated within objects
Data SecurityLess secure as data is exposedMore secure due to encapsulation
FocusFocuses on functions (procedures)Focuses on objects
Code ReusabilityLess reusableHigh reusability through inheritance
InheritanceDoes not support inheritanceSupports inheritance
PolymorphismDoes not support polymorphismSupports polymorphism
ExamplesC, PascalC++, Java, Python

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 Car class.

You can define a class using the class keyword.

# Defining an empty class
class Car:
pass
# Defining a class with attributes
class Car:
brand = "Toyota" # This is a class attribute
price = 1000000

In 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 object
print(my_car.brand) # Output: Toyota

Here, 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 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 self keyword. If an instance and a class attribute have the same name, the instance attribute takes precedence for that specific object.

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 = brand
    self.color = color
    my_car = Car("Toyota", "Red")
    print(my_car.brand) # Output: Toyota