What are “Classes” in programming and how are they distinct from functions? What about their applications?


What are “Classes” in programming and how are they distinct from functions? What about their applications?

In: 10

A class, in object-oriented programming, can best be thought of as a template for an object that can hold various variables and functions that “belong” to it.

You can have an Automobile class that has variables like its mileage, fuel capacity, whether it’s front- or rear-wheel drive, as well as functions like start(), shiftGears(), useRadio(), and others.

Then, you can create an Automobile object, and it automatically has its own variables and methods that you can independently interact with.

Imagine a class like a blueprint of some object: like a house. Your code defines a “house” blueprint that has some attatched properties and functions, and then you can use that blueprint to create as many houses as you want. Because you know what the blueprint contains you can know what functions and properties every house has even if each induvidual house may have different values. Every house could have a “color” value that tells you what the color of it is, or a “rooms” value to store all the rooms of the house, and a “open()” and “lock()” function to affect if the front door is locked or not, and you can rely on the houses having those things because they were created from a house blueprint that say they have these things.

A function is just a block of code that you’ve given a name: you call it and it does something. A Class can have multiple functions attatched to it, but it also stores data. The idea being that if you have a Class you can create an instance of that class, and that instance is a convinient little package that has all the relevant code directly built in, or can be the foundation for a derived class.

The difference depends on if you’re talking method or actual functions.

In practice a class is an object (separate file with separate logic from another class). Typically a function is used in non-object oriented programming, but is used to separate tasks or actions in different parts of the same file. Note, methods in object-oriented programming are similar but a little different and typically work for the class they’re apart of

The big difference is that a class IS something and a function DOES something. A class can contain a bunch of IS somethings or DOES somethings (methods or functions). A function is purely the DO of something.

Class combine functions (what something can do) with data (what something’s state is) together to help you better organize and create programs.

Classes act as blueprints for creating objects, which are specific instances of a class.

For example let’s create a Person class. We’ll keep it simple and give it two variables, firstName and lastName, and one function introduceSelf().

We know that any instance of the Person class has a first and last name and can introduce itself.

We could create two Person objects, let’s say the first one, which we’ll call personA is given the name Bill Jobs. We’ll create a second one, personB and give it the name Steve Gates.

If we tell personA to introduce itself it’ll say “Hi I’m Bill Jobs.”

If we tell personB to introduce itself it’ll say “Hi I’m Steve Gates.”

Both objects that were created using the Person class blueprints have the same action and they use their individual data to perform that action.

By combining data and actions together we can build logical models that make it easier to write complex programs. It can make it easier for others to understand those programs and it can also make it easier to share that code so other people can use it in their own programs.

A function is a basic abstraction. A reusable bit of code you use to accomplish a specific thing. Rather than having to retype that code every time you want to use it, you name it, so when you want to use it, you call it by that name, pass it the information it needs to do its job, and it gives you back a result. And that’s that.

A class is a more complicated abstraction. Instead of just a reusable bit of code, it has a degree of “intelligence”. When you create an instance of a class (an object), it sticks around. You can interact with it. It keeps track of scope and state. It can do things for you, and prevent you from doing others. It can decide who, when and how the data it contains can be viewed or changed. It can even instantiate other objects, an need perform other tasks.

That’s kind of vague, and really surface level. You very quickly move into things like interfaces, abstract classes, inheritance, and other concepts that are confusing at first – but the tl;dr; is that they are different kinds of abstractions. Functions are simple and direct. Basic building blocks, or simple machine…a lever, or incline plane. Classes are a more advanced implementation… building, or a watch, or a logical engine.