Class and Object

Updated Nov 30, 2025

Class and Object

Class and Object are the foundation of Object-Oriented Programming (OOP). Think of a class as a plan to make something, and an object as the real thing created from that plan.

alt OOP car example

What Is a Class?

A class is like a Plan to manufacture a car.

  • It tells what parts the car will have
  • It tells what the car can do
  • But it is not a real car

What Is an Object?

An object is the actual car made using that plan. This real car has:

  • color
  • company
  • model

And the real car can do:

  • start
  • stop
  • accelerate
  • honk

So:

  • Class = Plan (not real car)
  • Object = Real car you can use

What Are Attributes?

Attributes are the information that an object HAS. They tell you what the object is like.

Think of attributes as:

  • What the object HAS
  • The information about the object
  • What makes each object different from others

Example for a Car:

  • color - "Red", "Yellow"
  • model - "Cooper", "City"
  • company - "MINI", "Honda"

Each car object can have different attribute values!

What Are Methods?

Methods are the actions that an object CAN DO. They tell you what the object can do.

Think of methods as:

  • What the object CAN DO
  • The actions the object can perform
  • What you can ask the object to do

Example for a Car:

  • start() - starts the car engine
  • stop() - stops the car engine
  • accelerate() - makes the car go faster
  • honk() - makes the car make a sound

All cars (objects) can do these same actions, but each car does it with its own information!

Simple Car Diagram

    +--------------------------+
    |           CAR            |
    +--------------------------+
    | Attributes (HAS)         |
    |  - color                 |
    |  - model                 |
    |  - company               |
    +--------------------------+
    | Methods (CAN DO)         |
    |  - start()               |
    |  - stop()                |
    |  - accelerate()          |
    |  - honk()                |
    +--------------------------+

This diagram shows:

  • Attributes (top section) - What the car HAS
  • Methods (bottom section) - What the car CAN DO

Python Example

class Car: def __init__(self, color, model, company): self.color = color self.model = model self.company = company def start(self): print("Car has started") def stop(self): print("Car has stopped") def honk(self): print("Beep Beep!") # Creating objects (real cars) car1 = Car("Red", "Cooper", "MINI") car2 = Car("Yellow", "City", "Honda") car1.start() car2.honk()

What this code does:

  1. Defines the Class (Plan):

    • class Car: - This is the plan for making cars
    • __init__ - Sets up what each car HAS (color, model, company)
    • start(), stop(), honk() - These are what each car CAN DO
  2. Creates Objects (Real Cars):

    • car1 = Car("Red", "Cooper", "MINI") - Makes a red MINI Cooper
    • car2 = Car("Yellow", "City", "Honda") - Makes a yellow Honda City
  3. Uses the Objects:

    • car1.start() - Makes car1 start
    • car2.honk() - Makes car2 honk

Output:

Car has started
Beep Beep!

🎉 End

Classes and Objects are the basics of Object-Oriented Programming. Once you understand that a class is a plan and an object is the real thing, you're ready to learn more OOP concepts like Inheritance, Encapsulation, Polymorphism, and Abstraction!