Practicum Bootcamp
Published in

Practicum Bootcamp

Classes in Object-Oriented Programming

Diving deep into one of the most intricate and perplexing concepts in web development.

This series of articles is dedicated to object-oriented programming. As this is the most popular programming paradigm, it follows that anyone who takes programming seriously and wants to become a true web developer should develop a solid understanding of OOP.

If you haven’t read the first article, no worries. Let’s briefly summarize what we learned there.

OOP is a programming paradigm. That is to say, it’s a set of techniques and principles that web developers usually take advantage of.

First, let’s contrast this approach with procedural programming. Procedural programming operates using functions to perform a variety of different tasks. We can call our functions using additional functions, as needed. In a program, functions and data are individual entities.

The big disadvantage with procedural programming comes from the difficulty of creating and maintaining large projects. Even a fairly complex project will require dozens of functions connected to each other, resulting in unstructured code referred to as “spaghetti code”. When code connects to different parts of code it becomes tangled and disorganized, resembling a big bowl of wild and untamed spaghetti.

In OOP, all functions and data are encapsulated in the form of “objects”. Objects are relatively independent and interact with each other according to certain rules.

Inside objects, we can store data as “properties”. For example, the object can have a property called with a string value of . As for functions, they are also stored inside of objects and referred to in this context as "methods". The property can have a method which will send an email to a certain user.

Object-oriented programming helps us avoid spaghetti code. Let’s draw an analogy with real spaghetti! OOP takes a huge pot of spaghetti and allows us to serve it in a number of different food containers. That’s right, spaghetti, warm and nicely encompassed inside each container. Likewise, each programmer will work on a specific piece of code without getting their hands dirty from other pieces of code. Keep your spaghetti under control, everyone.

Now, back to the topic of the day — classes. One of the advantages of OOP is that you don’t need to write the same code over and over again. Instead, we can create a simple template, reusable with one line of code. These reusable templates for creating objects are called classes.

What are classes?

A class defines the data and functionality that an object possesses, as well as how it should be implemented.

Simply put, a class is a blueprint of code for creating objects. It’s similar to furniture assembly instructions or a recipe in a cookbook. The class itself doesn’t do anything other than help us create a new object and use it in our code.

To better understand how classes work, let’s consider an example. Imagine you’re in charge of cell phone production and you’re planning to introduce a new model to the market. To make sure people can instantly start working with your device, your phone needs to have a screen, a power button, volume buttons, a front and back camera, a charging port, and a SIM card slot.

Apart from having all this hardware, it’s crucial to connect everything together so that the phone works smoothly without any glitches. Additionally, you need to consider what should happen when a user clicks a certain button and how the user will make use of the phone.

The next step is to describe each and every component of the phone itself, like the screen, any microchips, and the motherboard. Next, it’s important to explain how all these components should interact with each other. The final step is to write a user guide describing the main functions of the phone, for example, how to call someone or how to send a text.

This is how we can make a new class for our phone. The class represents a full set of descriptions, properties, attributes, and instructions that describe our phone’s model. The class is not the phone itself, just the manual for using it.

In programming, classes have data. In this example, the data would be the phone’s components. There are also functions given — methods — which are used to interact with objects created from these classes. Methods define how users will interact with the phone.

Classes in practice

Python is a modern object-oriented language, and is perfect to illustrate what we’re talking about with classes. Let’s try it in practice.

Imagine you’re creating an online shop with a discount system. This means that it’s likely that you’ll frequently interact with regular customers. We’ll need to create a class that will help us initialize new customers. In this case, we’ll create a class called with the , , and a default shipping :

Inside the code above, you’ll see the keyword repeated a few times. In Python, this is used as part of class initialization, and it's beyond the scope of this simple introduction. For now, it's enough to just copy the format we're using.

We’ve also added two lines here. The first line declares a counter class variable: . The second line will increment this counter each time a new instance of the class has been created: . This counter isn't strictly necessary, but it can be quite helpful. We can reference this variable to check how many instances of our class we've created.

If that’s all a bit too technical, let’s just break it down further. In plain English, the code above says, “Here’s a class for customers. It has three properties: , , and a shipping ."

Thanks to our class, we can now create a new customer using one line of code. Let’s make a couple now, and we’ll check the counter variable while we’re at it:

What’s next?

In the next article in the series on OOP, we’ll model a real-life situation by adding a loyalty program and bonus points to our online shop. We’ll also talk about how Python can help us with this. And, to make it even more fun, we’ll write code both in Python and JavaScript.

We’re pushing onward in our series on OOP, but if you want to tackle this even more in-depth, why not go ahead and change or upgrade your career at the same time?

At Practicum, we offer online education and mentorship to help you pursue a career in tech. You’ll get deep into interesting topics like OOP, web development, data science, and a lot more. Plus, you’ll do it while working in an awesome and supportive environment with both peers and mentors.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store