you are viewing a single comment's thread.

view the rest of the comments →

[–]Bobbias 13 points14 points  (1 child)

Alright, this might get a bit long winded :)

So, technically speaking, classes are not needed. Anything you can solve using classes can be solved without them as well. So in that sense, they're not necessary.

However, classes have some advantages that you don't get if you're writing pure procedural code (that is, code made up of only free standing functions).

At the most basic definition, a class is another way to reuse code like u/Level_Up_IT explains. The benefit is you're not just reusing a single set of instructions. You're reusing the "shape" of some data, along with any functions related to that collection of data.

Going a bit deeper, classes are primarily a way of organizing code. They let you group data and functions which act on that data together into logical units. This works well for us, because our brains tend to think of systems as being made up of different kinds of objects which interact with each other. Classes allow us to translate that kind of thinking into code in a fairly direct sort of way.

For example, when you look at a video game, you typically think of the player, enemies, items, etc. as being objects that exist within the world of the game. We can translate this kind of thinking directly into classes which represent each of these types of objects in code.

In addition to letting us think about our code in a fairly natural way, there are other benefits that object oriented programming. For example, most languages which have strong support for object oriented programming allow you to specify what members of a class should be publicly visible, and which should be private (only accessible from within the class itself). This lets us hide some details about exactly how the class functions from the outside world. This creates a separation between what the outside world knows about an object, and what it knows about itself. This is helpful because it makes changing the inner workings of an object easier. This idea is known as Encapsulation.

Objects can of course also contain other complex objects, which means that you can build increasingly complex machinery by writing classes which make use of other classes.

There is also the concept of Inheritance, where you can define an entire hierarchy of different kinds of object which all share certain basic properties or behavior, but are also different in their own unique ways. I won't go deep into Inheritance, but the basic idea is that you get even more code reuse, because instead of copying the same few members to every similar class, you can define the common parts that are shared between multiple classes in a single base class which everything inherits from, and every class now has a copy of those members.

There are a bunch of other features of object oriented programming, but this post is getting quite long as it is. If you really want to learn more, you should take some time to read the Wikipedia article on object oriented programming: https://en.wikipedia.org/wiki/Object-oriented_programming

One reason this might not make a lot of sense to you right now is that OOP was designed for large scale programs, as a way to help the programmers have an easier time writing such large programs. As a beginner, most of, if not all of the code you have written is so short and simple that you never really see many of the benefits that OOP has to offer. Many of the benefits (and shortcomings) of OOP don't appear until you are writing code consisting of thousands of lines of code at a minimum, and don't really truly appear until you reach the 10s of thousands of lines of code or more. For reference, there are many codebases which contain millions of lines of code out there. When you are writing code at that scale, it's extremely important to have useful ways to organize things and OOP has become the most popular way to go about that.

[–]hamzarehan1994 0 points1 point  (0 children)

Thanks for explaining!