šŸ

Object-Oriented Programming: Classes and Objects

Intermediate
100 XP
45 min
Lesson Content

Object-Oriented Programming: Classes and Objects

Object-Oriented Programming (OOP) is a programming paradigm that organizes code into objects. Python is an object-oriented language, and understanding OOP is crucial for building complex applications.

What are Classes and Objects?

  • Class: A blueprint or template for creating objects
  • Object: An instance of a class (a specific example)

Think of a class as a cookie cutter and objects as the cookies made from it!

Defining a Class

class Dog:
    # Class attribute (shared by all instances)
    species = "Canis familiaris"
    
    # Constructor method (runs when object is created)
    def __init__(self, name, age):
        # Instance attributes (unique to each object)
        self.name = name
        self.age = age
    
    # Instance method
    def bark(self):
        return f"{self.name} says Woof!"
    
    def get_info(self):
        return f"{self.name} is {self.age} years old"

# Create objects (instances)
dog1 = Dog("Buddy", 3)
dog2 = Dog("Max", 5)

print(dog1.bark())  # Buddy says Woof!
print(dog2.get_info())  # Max is 5 years old

The __init__ Method

The constructor method that initializes new objects:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        print(f"{name} created!")

person = Person("Alice", 25)  # Alice created!

Instance Methods

Methods that operate on instance data:

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
    
    def area(self):
        return self.width * self.height
    
    def perimeter(self):
        return 2 * (self.width + self.height)

rect = Rectangle(5, 3)
print(rect.area())  # 15

Class vs Instance Attributes

class Counter:
    # Class attribute (shared)
    count = 0
    
    def __init__(self):
        # Instance attribute (unique)
        self.value = 0
    
    def increment(self):
        self.value += 1
        Counter.count += 1  # Modify class attribute

Why Use OOP?

  • āœ… Organization: Group related data and functions
  • āœ… Reusability: Create multiple objects from one class
  • āœ… Maintainability: Easier to update and debug
  • āœ… Abstraction: Hide complexity behind simple interfaces
Example Code

Create a Student class with name, age, and grade attributes, plus methods to display info.

class Student:
    def __init__(self, name, age, grade):
        self.name = name
        self.age = age
        self.grade = grade
    
    def display_info(self):
        return f"{self.name}, age {self.age}, grade: {self.grade}"
    
    def is_passing(self):
        return self.grade >= 60

# Create and test
student = Student("Alice", 20, 85)
print(student.display_info())
print(f"Passing: {student.is_passing()}")

Expected Output:

Alice, age 20, grade: 85
Passing: True
Study Tips
  • •Read the theory content thoroughly before practicing
  • •Review the example code to understand key concepts
  • •Proceed to the Practice tab when you're ready to code