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 Scope→→Linked 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 Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Introduction to Classes

Longest path: 79 steps · 328 total prerequisite topics

Prerequisites (2)

Leads To (3)