What is the difference between a class and an object?

Question

What is the difference between a class and an object?

Answer

Think of a class as a blueprint and an object as the result of creating what that blueprint specified.
Classes allow us to define a general state and behavior for the objects we want to make. This is a super powerful way to approach programming because it allows for us to generalize our code to work for many situations!
For example, we might make a Car class that has things you might find in any car: wheel_count, gas_tank_size, and a seat_count of at least one for the driver to sit in. When we create an object of type Car, we might make a mustang object (notice it’s lowercase, as is convention when instantiating objects of a class). Our mustang has its own values for those variables because it’s different from other Cars, but still has a lot of the same properties. We could make an object for any model of car!
Same basic structure, different values. That’s the use of a class!

8 Likes