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

Methods and Attributes

College Depth 80 in the knowledge graph ☐ I know this ☆ Set as goal
528topics build on this
330prerequisites beneath it
See this on the map →
Introduction to ClassesObjects and Instances→→Algorithm Design Basics
methods attributes self dot notation encapsulation

Core Idea

Attributes are the data stored in an object (accessed with dot notation: obj.attribute). Methods are functions defined inside a class that operate on the object's data, receiving the object itself as their first argument (self). Getter methods expose attribute values; setter methods validate and update them. The combination of attributes and methods implements the object's interface — what the outside world can ask the object to do or report.

How It's Best Learned

Add deposit(), withdraw(), and get_balance() methods to a BankAccount class. Enforce invariants in setters (e.g., balance cannot be negative). Access attributes via methods rather than directly to practice encapsulation.

Common Misconceptions

Explainer

You already know that an object is an instance of a class, carrying its own data. Now let's look at the two building blocks that make objects useful: attributes store what an object *knows*, and methods define what an object can *do*. Together, they turn a passive data container into something that behaves.

Attributes are variables that live inside an object. You access them with dot notation: `account.balance`, `player.health`, `car.speed`. Each object has its own copy — changing `car_a.speed` does not touch `car_b.speed`. Attributes are typically set during construction (in the `__init__` method in Python, or the constructor in Java/C++) and then read or modified throughout the object's lifetime. They represent the object's state at any given moment.

Methods are functions defined inside the class that operate on that state. The critical difference between a method and a standalone function is that a method automatically receives a reference to the object it was called on — in Python this is the `self` parameter, in Java/C++ it is the implicit `this`. When you write `account.deposit(50)`, the `deposit` method receives `self` pointing to `account`, so it can access `self.balance` and modify it. This is why forgetting `self` in a Python method definition causes confusing errors — without it, the method has no way to reach the object's attributes.

A well-designed class controls access to its attributes through methods rather than letting outside code modify attributes directly. For example, instead of writing `account.balance = account.balance + 50` (which skips any validation), you call `account.deposit(50)`, and the method can check that the amount is positive before updating the balance. A `withdraw` method can verify sufficient funds before reducing the balance. These are examples of getter and setter patterns: methods that read or update attributes while enforcing rules. This idea — that the object controls how its own data is accessed and changed — is the beginning of encapsulation, one of the central ideas in object-oriented programming. The object's methods form its public interface: the set of operations the outside world can ask it to perform, without needing to know the internal details of how attributes are stored or managed.

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 → Objects and Instances → Methods and Attributes

Longest path: 81 steps · 330 total prerequisite topics

Prerequisites (2)

Leads To (1)