OOP Part 1: Getting Started

Get to know the basics of OOP

OOP Part 1: Getting Started

Welcome to the introduction to OOP. In this chapter you will:

  1. Get to know what OOP is
  2. Read a simple real-world example of OOP
  3. Do a 3-Question quiz to test your understanding

This guide is therefore structured in 3 parts: Explanation, Example and lastly an Exercise. This gets you learning as quickly as possible.

Sounds good? Let's jump into OOP!


PS. Want to watch instead of reading? have a look at the YouTube version of this guide:

1. What Is OOP?

The Explanation

Object-Oriented Programming is a programming paradigm.

A Programming Paradigm is a way of thinking about and structuring code. It defines the style, techniques and rules for writing code and programs. Think of it as a philosophy of programming - Different paradigms express different ways to write and structure code.

Up until now, you've likely been writing procedural code - A style where programs are structured as a series of instructions that the computer executes step by step.


How You Normally Write Code

In a simple program, you might store students' names, ages, and grades in separate lists and write functions to process them.

3 Student Info Lists & A Function For Printing

This works for small projects, but as your program grows, managing multiple lists becomes messy and error-prone. Functions must constantly reference and update different lists, making it harder to track relationships between data.

For example, if you want to update a student's grade, you have to manually find the correct entry in multiple lists. This increases the risk of bugs and inconsistent data.

This is where OOP helps—by grouping related data and behaviors together.


How You Will Write Code After Completing This Guide

In contrast, OOP introduces the idea of bundling data and behavior inside objects. Instead of keeping track of multiple lists, you define a Student Class, where each student is an object that already contains the attributes and functionality you need.

Here's what it might look like:

A Student Class With Student Attributes & A Method For Printing

Don't worry about not understanding the entire code. Just note that in OOP, the variables (name, age and grade) and the functions associated with students are contained within the same unit: The Class.

This links data with their intended behaviour, whether that be printing, increasing grade or increasing age. It makes the code:

  • cleaner
  • scalable
  • and easier to manage

3 Reasons to Use OOP

OOP is incredibly popular among developers, and for good reason. Here are the top 3 reasons why OOP is useful and why you should learn it.

Organization

OOP code is easier to understand and organize. All relevant attributes and functionality of the student class above are found in one place. If you wrote it useing procedural programming, new student data could be added from anywhere in the program, making it hard to keep organized.

💡 Think of OOP like organizing files in labeled folders instead of scattering papers all over your desk.

Reusability

OOP promotes code reusability through classes. Instead of rewriting the same logic twice (or sometimes way more), you can define a class once and reuse it throughout your program, even in different projects.

💡 OOP is like using blueprints - Once you design a blueprint, you can build as many copies (objects) as you want.

Maintainability

Changing code, which is inevitable in large projects, is tough.

Often, you have to rethink the code structure which you first had in mind. OOP makes these kind of changes easier because all relevant data and functionality is stored in one place.

💡 OOP makes debugging and extending functionality smoother because changes are made in one place, reducing errors and unexpected side effects.

The 4 Components of OOP

Object-Oriented Programming is built on four key components. Here's a brief explanation on each one:

Classes - The Blueprint

A class is a blueprint for creating objects. It defines what data an object should have (attributes) and what actions it can perform (methods).

A simple class might look like this:

A Car Class with a function for defining the attributes & one for defining what objects can do (accelerate)

Objects - The Actual Things You Use

An Object is a real instance of a class. It's what you actually use in your program.

Using our Car Class, we can now create multiple car objects:

Two instantiations of the car class

Each object has its own unique data, but follows the same structure defined by the car class.

💡
If the class is the cookie cutter, the object is the cookie

Methods - What an Object Can Do

A method is just a function inside a class.

It defines what actions an object can perform. In our Car Class, accelerate() is a method because it lets cars increase their speed. But we can add more methods like this:

Brake() Method Added

Attributes - The Data Inside an Object

Attributes store an object's information. They are the variables inside a class that describe the object.

Here are some examples of attributes being set and used:

Example - A Car Class and Object

2. A Car Class and Object

The Example

Now that you understand the basics of OOP, let's recap with a full example.

We're going to use the car class and object parts that we just went through. But now with the full code:

Read through the code and see if you know what's going on

What Just Happened?

  • We created a class (Car) that defines:
    • Attributes: brand, model and speed
    • Methods: accelerate, brake
  • We made two car objects (car1 and car2), each with its own data.
  • We used methods to make the cars speed up and slow down.
  • Simple Use Case: The for-loop simulates a short drive by making car1 accelerate three times, then slow down twice.
💡
Each object follows the same rules but operates independently. This is the power of OOP.

Now that you've seen a complete example, let's test your understanding with a 3-Question Quiz.

3-Question Quiz

The Exercise

Test Your Understanding With These 3 Questions

1️⃣ What will happen if you run the following code?

Assume that the Car Class is already defined

A) The car will stop completely at 0 km/h

B) The car's speed will decrease by 10 twice, stopping at 20 km/h

C) The car will keep slowing down forever

Click For Answer

Answer: B

The car starts at 40 km/h, and brake() decreases the speed by 10 each time. After two brake() method calls, the car will be at 20 km/h.


2️⃣ What happens if you create multiple objects from the same class?

A) They share the same attributes and change together

B) Each object has its own attributes and can behave independently

C) You can only create one object per class

Click For Answer

Answer: B

Each object is unique and has its own data.


3️⃣ What will this code print?

Again, assume that the Car Class is already defined

A) Ford Mustang is now going at 40 km/h

B) Ford Mustang is now going at 30 km/h

C) Error: accelerate() requires an argument

Click For Answer

Answer: A

The speed increases by 10, so it becomes 40 km/h.


Moving on...

If you got these questions right - Awesome! You're already thinking in objects and getting comfortable with OOP fundamentals.

If you missed some, don't worry. The next chapter will dive deeper into Classes and Objects - so that you'll be able to create and design your own classes for use in your projects.