Skip to main content

Side by side

ClassvsObject

What is the difference between a class and an object?

Updated 2 min read7 differences

In short

A class is a blueprint that describes what data and behavior a kind of thing has, while an object is one concrete thing built from it, with its own values.

Class

A class is a blueprint in object-oriented programming that defines the data and behavior shared by a group of objects, which are created from it as instances.

Read the page on Class

Object

An object is a bundle of related data and behavior: values in named fields, often called properties, and functions that work on them, called methods.

Read the page on Object

Class and Object compared

AspectClassObject
What it isA blueprint or typeOne concrete instance of that type
ExistsIn the code, before the program runsIn memory, while the program runs
How manyOne definitionAs many as the program creates
HoldsDefinitions of fields and methodsActual values for those fields
Created byWriting it in the source codenew, or calling the class's constructor
MemoryIts definition is loaded onceEach object takes its own memory
ExampleUser, Order, ButtonAda's account, order 1042, the Save button

The difference, explained

In object-oriented programming, a class defines a type: the fields its objects will hold and the methods they can run. An object, also called an instance, is created from a class and holds actual values. User is a class; the user named Ada with the email ada@example.com is an object of that class.

A class is written once, in the code, and exists before the program creates anything from it. Objects are created while the program runs, usually with new or by calling the class, and there can be as many as needed: one class Order, thousands of order objects. Each object keeps its own state, so changing one order's total doesn't touch the others, while all of them share the same methods.

The usual analogy is a cookie cutter and the cookies: the cutter decides the shape, and each cookie is a separate thing you can decorate differently. Or a house plan and the houses built from it: one plan, many houses, each with its own address and furniture.

The words blur in some languages. In JavaScript, classes are mostly a cleaner syntax over prototypes, and you can create objects without any class at all with { }. In Python, a class is itself an object that can be passed around. The idea stays the same: the class describes, the object is.

Which one should you use?

Choose Class when…

  • You are describing what every thing of one kind has and can do.
  • Several places in the code need things with the same shape and behavior.
  • You want to define a new type, with its rules kept in one place.

Choose Object when…

  • You need one particular thing with its own values, such as this user or that order.
  • The program is running and has to hold, change or pass around data.
  • You are working with data that exists right now, not with its definition.

One class, two objects

Classpython
# The class: written once, describes every account
class Account:
    def __init__(self, owner):
        self.owner = owner
        self.balance = 0

    def deposit(self, amount):
        self.balance += amount
Objectpython
# The objects: created at runtime, each with its own state
ada = Account("Ada")
linus = Account("Linus")
ada.deposit(50)

print(ada.balance)    # 50
print(linus.balance)  # 0, untouched

Readers ask

Is an instance the same as an object?

Yes, in everyday use. "Instance" stresses which class the object came from, as in "ada is an instance of Account", but both words mean the same thing.

Can there be objects without classes?

Yes. In JavaScript you can write an object literal such as { name: "Ada" } with no class at all, and some languages are built around prototypes instead of classes. Classes are one way to create objects, not the only one.

Do all objects of a class share data?

Each object has its own fields, but a class can also have static fields that belong to the class itself and are shared by everything, such as a counter of how many objects were created.

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings