CodingNic

Object-Oriented Programming

Introduction to OOP

Object-Oriented Programming 24 min read

Introduction to OOP

Introduction to OOP

As programs become larger, code can become messy.

You may have many variables, many functions, and repeated code.

Object-Oriented Programming helps organize programs better.

OOP uses classes and objects.

What Is OOP?

OOP stands for Object-Oriented Programming.

It is a way of writing code by grouping related data and actions together.

Real World Example

Think about a car.

A car has:

  • data: color, brand, speed
  • actions: start, stop, move

In OOP:

  • data = attributes
  • actions = methods

Why OOP Matters

OOP helps you:

  • organize code
  • reuse code
  • avoid repetition
  • model real things
  • build bigger programs

Without OOP

python
car_name = "Toyota"
car_color = "Blue"

def start_car():
    print("Car started")

start_car()

This works, but related data is separate.

With OOP Idea

python
class Car:
    pass

A class is a blueprint.

It describes what objects will look like.

What Is an Object?

An object is a real item created from a class.

Example:

  • Class = Car blueprint
  • Object = one real car

Real Examples

  • Class: Student → Object: Tom
  • Class: Product → Object: Laptop
  • Class: User → Object: admin user

First Simple Class

python
class Dog:
    pass

This creates a class named Dog.

Create an Object

python
class Dog:
    pass

pet = Dog()

print(pet)

Output Example

text
<__main__.Dog object at ...>

The memory address may be different on your computer.

Why This Is Useful

Now you can create many dogs from one class.

Example

python
dog1 = Dog()
dog2 = Dog()

print(dog1)
print(dog2)

Each object is separate.

Code Along

Create a class called Book.

Create one object named my_book.

Print it.

Mini Challenge

Create a class called Phone.

Steps:

  • Create the class

  • Create two objects:

    • phone1
    • phone2
  • Print both objects

Expected output:

text
<__main__.Phone object at ...>
<__main__.Phone object at ...>

Real World Use Case

Apps use classes for users, products, messages, orders, vehicles, and game characters.

Quiz

  1. What does OOP stand for?
  2. What is a class?
  3. What is an object?
  4. Why is OOP useful?

Assignment

Create a class called Student and make three student objects.

Summary

You learned that OOP organizes code using classes and objects, making programs cleaner and easier to manage.