A class is a blueprint for creating objects. It defines what data an object holds and what it can do — all in one place.
Classes are the main building block of object-oriented programming. Instead of writing all your code in one file, you split it into classes — each one responsible for one thing.
Defining a Class#
A class has a name and a body. The body contains the data and behaviour that belong to it. The exact syntax depends on the language — see the language-specific pages below.
Adding a Method#
A method defines what an object can do. Methods are declared inside the class body and called on objects.
Creating an Object#
A class is just a blueprint until you create an object from it — an actual instance in memory. Creating an object from a class is called instantiation. You typically use a keyword or call the class directly, depending on the language.
Language-Specific Pages#
Common Mistakes#
Confusing a class with an object
A class is the blueprint; an object is the thing built from it. Defining a Player class doesn’t create a player — you have to instantiate it explicitly.
Putting everything in one class Object-oriented programming only helps if each class has a clear, single responsibility. A class that handles game rules, player input, and score display all at once defeats the purpose. When a class is doing too many things, it’s a sign it should be split.
Defining a class inside another class unintentionally Most languages allow nested classes, but this is almost never what a beginner intends. Keep class definitions at the top level of the file.
Calling a method without creating an object first A method belongs to an object. You must instantiate the class before calling any of its methods.
Resources#
- Class (computer programming) (external link) — Wikipedia