Chapter 2: Classes and Objects in OOP

Chapter 2: Classes and Objects in OOP

Abstract:
gramming (OOP), classes are blueprints for objects, and objects are instances of those classes. Classes define the structure and behavior of objects, while objects are specific implementations of those classes. 
Classes 
  • User-defined data types that define the structure and behavior of objects
  • Contain data members and member functions
  • Can be thought of as a "type"
  • Can define initial values for state (member variables or attributes)
  • Can define implementations of behavior (member functions or methods)
Objects 
  • Instances of a class that are created with specifically defined data
  • Can correspond to real-world objects or an abstract entity
  • Can have different property values
  • Can be created many times as per requirement
  • Can be created using the "new" keyword
Example
  • A class called "Car" can be used to create objects like BMW, Mercedes, and Ferrari 
  • A class called "Person" can be used to create objects of the type "person" 
Related concepts 
  • Inheritance, a mechanism of reusing code
  • Encapsulation
  • Polymorphism

2.1 Introduction

Object-oriented programming (OOP) is a fundamental paradigm in modern software development, emphasizing modularity, reusability, and abstraction. At the core of OOP are classes and objects. A class is a blueprint for creating objects, and an object is an instance of a class. This chapter explores how to define classes, create objects, manipulate object data, and utilize constructors and destructors.

2.2 Defining a Class with Attributes and Methods

A class is defined using the class keyword, and it typically contains attributes (data members) and methods (functions). Attributes store the state of an object, while methods define its behavior.

2.2.1 Syntax for Defining a Class

class Car:
    def __init__(self, brand, model, year):
        self.brand = brand      # Attribute
        self.model = model      # Attribute
        self.year = year        # Attribute
    
    def display_info(self):
        return f"{self.year} {self.brand} {self.model}"

Here, Car is a class with three attributes: brand, model, and year. The method display_info() returns a formatted string representing the car’s details.

2.3 Creating Objects as Instances of a Class

Once a class is defined, we can create objects (instances) of that class.

2.3.1 Object Creation

car1 = Car("Toyota", "Camry", 2022)
car2 = Car("Honda", "Civic", 2023)

Here, car1 and car2 are two objects of the Car class, each with specific values for brand, model, and year.

2.3.2 Accessing Object Attributes and Methods

We can access an object's attributes and methods using the dot (.) notation.

print(car1.brand)         # Output: Toyota
print(car2.display_info()) # Output: 2023 Honda Civic

2.4 Accessing and Manipulating Object Data

We can modify an object’s data by directly changing its attributes.

car1.year = 2025
print(car1.display_info())  # Output: 2025 Toyota Camry

Additionally, class methods can be designed to update object attributes in a controlled manner.

class Car:
    def __init__(self, brand, model, year):
        self.brand = brand
        self.model = model
        self.year = year
    
    def update_year(self, new_year):
        self.year = new_year

2.5 Constructors and Destructors

2.5.1 Constructors

A constructor is a special method that initializes an object's attributes when the object is created. In Python, the constructor is defined using __init__().

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

Each time an object of Person is instantiated, the __init__ method automatically executes, assigning values to name and age.

2.5.2 Destructors

A destructor is a special method that is called when an object is about to be destroyed, allowing resource cleanup. In Python, the destructor is defined using __del__().

class Person:
    def __init__(self, name):
        self.name = name
        print(f"{self.name} is created.")
    
    def __del__(self):
        print(f"{self.name} is deleted.")
p1 = Person("Alice")  # Output: Alice is created.
del p1                # Output: Alice is deleted.

2.6 Summary

  • Classes are blueprints for creating objects.
  • Objects are instances of classes with unique data.
  • Attributes store object data, and methods define behavior.
  • Constructors (__init__) initialize objects upon creation.
  • Destructors (__del__) handle cleanup when an object is deleted.

This chapter laid the foundation for working with classes and objects in Python. In the next chapter, we will explore inheritance and polymorphism, which further enhance object-oriented programming principles.

Comments