A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Introduction to Classes

College Depth 78 in the knowledge graph I know this Set as goal
663topics build on this
328prerequisites beneath it
See this on the map →
Defining and Calling FunctionsVariable ScopeLinked ListsMethods and Attributes+1 more
classes OOP blueprints object-oriented encapsulation

Core Idea

A class is a blueprint that defines the structure and behavior shared by a family of objects. It bundles data (attributes) and functions that operate on that data (methods) into a single unit. Defining a class does not create an object; instantiating it does. Classes support encapsulation — hiding internal data and exposing only a clean interface — which reduces the surface area of bugs and makes programs easier to maintain.

How It's Best Learned

Design a simple class for a real-world concept (e.g., BankAccount, Student, Rectangle) with a few attributes and methods. Instantiate multiple objects from the same class and verify that each has independent state.

Common Misconceptions

Explainer

From your work with functions and variable scope, you know how to organize code into reusable blocks and how variables live within defined boundaries. Classes take this organization one level higher by bundling related data and the functions that operate on that data into a single unit. Imagine you're writing a program to manage bank accounts. Without classes, you'd have separate variables (`account_balance`, `account_owner`, `account_number`) and separate functions (`deposit()`, `withdraw()`, `get_balance()`), all floating independently. With a class, these belong together as a coherent whole.

A class is a blueprint or template — it describes what data an object will hold (its attributes) and what it can do (its methods). Think of a cookie cutter: the cutter itself is the class, and each cookie you stamp out is an instance (an object). Defining `class BankAccount:` creates the template. Calling `my_account = BankAccount()` creates an actual object from that template — this is called instantiation. Each instance has its own independent copy of the attributes. If you create two BankAccount objects, depositing into one doesn't affect the other, just as two cookies cut from the same cutter are separate physical objects.

Methods are functions that live inside a class and operate on a specific instance's data. In Python, every method takes `self` as its first parameter, which refers to the particular object the method was called on. When you write `my_account.deposit(50)`, Python automatically passes `my_account` as `self`, so the method knows *which* account to update. This is what connects the function to the data — `self.balance += amount` modifies this particular account's balance, not some global variable. The `__init__` method (the constructor) runs automatically when you create a new instance, setting up the object's initial state.

The deeper principle at work is encapsulation: grouping data with the operations that make sense for that data, and presenting a clean interface to the rest of the program. Other code doesn't need to know *how* the BankAccount tracks its balance internally — it just calls `deposit()` and `withdraw()`. This separation means you can change the internal implementation later (maybe switching from a simple number to a transaction log) without breaking any code that uses the class. Classes become the fundamental building block of object-oriented programming, where you model your problem as interacting objects, each managing its own state and responsibilities.

Practice Questions 5 questions

Prerequisite Chain

Understanding ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesBoolean AlgebraBoolean Type and Truth ValuesComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraConditional StatementsDefining and Calling FunctionsFunctions: Decomposing ProblemsFunction Parameters and Argument PassingReturn ValuesVariable ScopeIntroduction to Classes

Longest path: 79 steps · 328 total prerequisite topics

Prerequisites (2)

Leads To (3)