Lessons › coding and computer science · taught by Leo
Object-oriented programming basics: objects and classes
Learn why objects bundle data and methods, how classes create objects, and how self and object state work.
Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.appWhy objects are useful
When a program gets bigger, it helps to group related stuff together.
An object is one little package of data and actions.
That makes code easier to understand and reuse.
Object = data + actions
An object is a bundle. It holds information, and it knows what it can do.
Think of an object like a toy robot. It has parts, and it can do actions.
In code, the parts are called attributes or fields, and the actions are called methods.
So object-oriented programming means organizing code around objects.
Predict: what belongs in an object?
Which pair best matches an object in OOP?
- data and methods
- loops and prints
- only numbers
Show answer
data and methods: Yes. Objects usually bundle data with methods.
A class is the blueprint
A class is the blueprint or recipe.
An object is one thing made from that blueprint.
So one class can create many objects with the same structure.
Read a class and object
Here the class is Dog. It defines what a dog object can do.
Then my underscore dog is one object created from the class.
Finally, we call bark on that object.
Quick check: class or object?
What does Dog() usually make?
- one object
- a class blueprint
- a loop
Show answer
one object: Right. Dog() creates one object, or instance.
Instance data stays separate
Different objects can share the same class but hold different data.
That is why each object can have its own name, age, or state.
The methods can be the same, but the data can change.
Why self matters
Inside a method, self means the current object. It tells the method which object's data to use.
When a method runs, it needs to know which object it belongs to.
That is what self means in many languages like Python.
It points the method back to the current object.
Methods act on data
A method is just an action that works on the object’s own data.
That is why methods are useful: they keep the behavior close to the data.
When the method changes the object, we say the object’s state changed.
Watch out: class data vs object data
Which choice is the safer default for a student's name?
- store it on each object
- store one name for all students
- do not store any data
Show answer
store it on each object: Yes. Personal data should usually belong to each object.
A tiny OOP checklist
First, define the class.
Second, make objects from it.
Third, keep data inside each object and use methods to work with it.
Your turn: pick the class idea
Which is the best class idea for OOP?
- Car
- blue color
- seven
Show answer
Car: Correct. Car is a good class idea because many cars share the same kind of data and behavior.
Recap: class to object
Use this checklist on your own.
A class is the blueprint.
An object is one created thing with data and actions.
Fields hold data, and methods do the work.