Hello, I am new to this community, and first time posting here. This is my very first blog which I published in Hashnode with the same tittle- The Good Old 'What Are Classes and Objects?' in plain English
Reposting it here, in case it's helpful for my fellow developers or maybe just a fun read for anyone looking for a way to divert mind from writing code all day. Anyway, thanks for stopping by.đ
Why Am I writing this:
(I am being kinda emotional here)
When I first started my CSE program, I was introduced to the concept of âclasses and objectsâ, and I understood nothingâŚ
I knew how to code. I loved coding. I knew that âif I write this, Iâll get that output.â I completed my entire Object-Oriented Programming course; by following instructions and writing code that worked. But deep down, I still didnât understand what classes and objects really meant.
I felt like it would be better if someone explained these technical terms in my own language - the way I think, the way I speak.
Years later, I finally understand the concept a little better (not completely, but enough to help someone else). So I decided to write down what I know now.
Maybe someone out there will find this helpful, the version I wish I had when I had just started.
Now, enough with the sappy backstory. Letâs dive into the real stuff.
Class & Object:
Okay, so youâve probably heard the phrase âa class is a blueprint for creating objects.â Itâs the common definition. But letâs take a moment to unpack what it actually means.
The class acts like a design plan in code. We use these âdesign plansâ to create âobjectsâ which have the characteristics of real- life things. These characteristics, if one wishes to sound technical, are referred to as 'attributes'.
We can also perform some calculations or actions by defining âmethods or member functionsâ on those objects âor using those objects as needed by our business logic.
Letâs try some analogies here. Suppose, you have a company of automobiles (yes, we are thinking big here). You manufacture cars like â sedans, SUVs, trucks, sports cars and so on. And you want to store the data (information) of your products (in this case, the cars) in the database. Since you know how to code (thatâs why you are here. AND YES, you can have a car company and know how to code all at once. Your life, you rules), you will write a blueprint/design plan âor in programming terms a class, which will represent all your real-life products with their distinctive characteristics.
So youâll write a class maybe called Car, it will have some characteristics (attributes) likeâ
- Model_name
- Color
- Size
- Engine_type
- Car_type
- Number_of_products
Now, letâs say, you want to perform an action. For example, whenever a new type of car is added, (maybe you just launched a new design of your SUVs), the number of your products should increase.
Sure, you could update the number manually. But you are a developer, and developers think in terms of automation and smart solutions. So, inside the class, you will write a member function or a method which will increase the number of products automatically.
Like this, you will have more methods or member functions according to your needs.
At this point, youâve just designed your plan.
Following this plan, when we instruct the CPU, it will create a virtual car (a Car type object), in the virtual world (inside your computerâs memory). This creating part happens with the help of the constructor . Another important concept of OOP.
One important thing to remember:
When you define a class, your blueprint, itâs just like writing down the design on paper. No memory is allocated for it yet. Memory is only allocated when you actually create an object (also called an instance) of that class.
Now, I am not writing any code here. Our old best friend âgoogleâ or our new best friend âchatgptâ can help you with the code. But I am writing the pseudo code to give you a way to visualize how we usually design our plans aka define our classes.
Class Car:
Attributes:
model_name
color
size
engine_type
car_Type
number_of_products
Method:
add_new_product():
number_of_products += 1
This is a simple class that says, âEvery car has a few qualities like model name, color, size, car type and engine type. And whenever a new car is added, the number of products increases by one.â
As for creating objects, we should know about âconstructorsâ. But for now, letâs just know the syntax.
#We have defined our âCarâ class before
# Now, we create a car object using the Car blueprint
my_car = new Car()
my_car.model_name = "Sedan X"
my_car.color = "Red"
my_car.engine_type = "Hybrid"
Here, my_car is the actual object - a (virtual) car created from the blueprint -Car. Weâre giving it some specific values: the model name, color, and engine type.
In real code, the syntax will depend on the programming language, but the idea stays the same: You're creating a thing (object) based on a plan (class) and filling in its details.
Finally, a quick recap:
A class is the design plan used to represent a real-life thing in the virtual world.
An object is that real-life thing, created based on the class, inside your computerâs memory.
No memory is allocated when we define the class (we just write the plan).
Memory is only allocated when we actually create an object.
So here it is, my simple explanation for classes and objects in plain English. I tried to keep it short. Hope it will be helpful.
I am thinking of writing my next piece on âconstructorsâ maybe. Who knows.
But till then, Adios!
Top comments (0)