What is a Class? What is an object? What is an instance?

Thulani Mtetwa
1 min readApr 27, 2022

--

After reading some articles and hearing some OOP (object, orientated programming) jargon thrown around I was curious about how is an object different from an instance. Well after reading this stack-overflow thread you can see how much confusion there is about these two.

Well, let's start with a class.

class Building() {
var isDoor = false
var numberOfWindows = 0

function openDoor -> Bool {
isDoor = true
return isDoor
}
function setNumberOfWindows(number: Integer) {
self.numberOfWindows = number
}
}

In the above class Building, we just defined the property and function of a building. Which is what a class is. A definition or template of a real-world thing (or in programming terms an object) or data structure.

Now the object.

var office = Building()var house = Building()var hotel = Building()

An object is created from a class. Now you can specify anything particular for each of object created like setting the number of windows for that building.

And lastly, the instance.

In programming terms, we refer to the object as and instance of the class, or just as instance. So following what we did in code in the above, office, house and hotel(objects) are instances of the building class.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Thulani Mtetwa
Thulani Mtetwa

Written by Thulani Mtetwa

I am an iOS App Developer. And worked on multiple apps in the finance and insurance industry.

No responses yet

Write a response