Use for Classes?

What is an example of what classes are used for?

Note that I won’t go through code here since you can google the topic or reference any book for the semantics/syntax for the specifics of creating classes.

You can think of classes as a software version of a stencil. Once you create one class, you can create many objects (instances) based on that class (stencil, template, or cookie cutter, etc.). However, because of inheritance and composition, you can create various unique versions of that class (or object). Something that you cannot do with a physical stencil.

For example, if you created a class for a car, you can then further tailor it for a specific type of car:

  1. Sedan
  2. Race car
  3. Jeep
  4. SUV
    n. etc.

A similar example might be when you get a new job. HR department will need your name (first, last), mailing address, SS number, job title, manager, and department (for example). When they enter this information into the system, the program that processes this information doesn’t have to have hundreds of copies of this same code for every employee. The program class just needs one main class along with inheritance and composition classes for specific types of employees (manager, supervisor, executive, reg employee, etc.). As you can see, classes work as templates for processing information. Classes also have their own internal attributes and methods for processing that data.

Another reason for classes is encapsulation. With classes, all of the object attributes and methods (functions) are encapsulated within the code from classes. This removes the potential for variables clashing when used in other program/package modules.

Here, I have highlighted but a few examples for using classes.

In Object-Oriented Programming we use classes to represent objects and assign them behavior as well as represent our data and the relationships between them.
Overall what you are asking is a very complicated topic and it is related to the fundamentals of programming, software design and architecture. To simplify it for someone who is just starting, you can think of it as grouping related things together so that they are always in one place and are accessible when you need them.

1 Like

So, you know how in your program you can have things that are ints, strs, lists etc.?

Classes are how, in Python, you create your own kind of thing, in order to make things that are of that kind. (In programming, we say “type” specifically. In ordinary English, words like “type”, “kind”, “category” all mean more or less the same thing; but for computer scientists, they have more precise meanings.)

1 Like

So it’s to assign behaviors all at once?

A class is like a template, it defines how an object will act. An object is an instance of the class. The class is just the definition of an object. But inheritance is where it gets neat to save time coding changes.

Inheritance is where one class has a “parent” of another class. If you change behavior in the parent class, the children classes all get that change without writing any more code.

Take the video game “Fallout 4”.


We can have a base class called “clsMonster”. This defines basic attributes that all monsters/opponents will share. It will have properties (which are like variables) common to all monsters. It will also have methods (which are functions) to define how the basic monster acts in combat, acts while idling, etc.

Now we base the “clsRaider” class on clsMonster. This will have “properties” which further define what the 3d model is, which textures are used on the 3d model, which audio files to use for certain events, hit point calculation, which weapons are commonly used, and it will have methods for how it works in combat and while idling.

Now we can have another class for Gunners called “clsGunner” based on clsRaider, and basically make it a much harder raider.

EDIT: And just practice. Experience will help you understand class hierarchies, and how different classes go from the general class parent, to a child class that uses the parent.

You can think this way, but it’s really a lot more complicated. I suggest reading up on the topic. My favorite book about it is " Design Patterns: Elements of Reusable Object-Oriented Software".

There is also some videos about it, as an example, this

The best way to understand imo is to try and code some somewhat complex project first without classes, and then rewrite it with a proper design and see what’s the difference is. I think something like: suppose you have a need to extract data from 4 different sources. One is MySQL database, one is Oracle, one is MongoDB and one is a RESTful api. You have either a form or web page where you need to display this data. Just displaying in a table would work, but you can add some kind of chart to it. On that form or web page you have a control or a menu that let’s you select what data to show.

Well, something like this. Make it without classes or design patterns, and then make one with a good design. You will see the difference.