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

300 views

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

In: 10

21 Answers

Anonymous 0 Comments

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.

You are viewing 1 out of 21 answers, click here to view all answers.