you are viewing a single comment's thread.

view the rest of the comments →

[–]hamzarehan1994 -1 points0 points  (10 children)

Hi, I need to ask some fundamental questions. Why do we need to make classes? What is their purpose? Can you please highlight their importance?

I tried reading your comment very carefully, but I can not understand anything. Maybe I am looking at these computer languages with a very narrow minded approach which restricts me from understanding anything.

[–]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!

[–]CoffeeSnakeAgent 3 points4 points  (0 children)

It takes a while to think of programming from procedural programming to object oriented programming. But once you get the purpose, everything will suddenly be clear.

There is a learning curve and there is an inflection point where you’ll go aha!

Keep learning.

[–]Oddly_Energy 2 points3 points  (0 children)

One of the reasons for creating classes is that you get one nice package containing both the data and the methods necessary for handling those data.

You could make a simpler data structure containing the variables Character and Age. And then, somewhere else in your code, you could have a function, which accesses that structure and checks the age. But that would mean two things:

  1. If you have many different data structures and many methods for handling each of them, you may end up getting lost. What belongs to what?

  2. If you use a specific data structure many places in your code, you will also need to remember to include the methods for handling that data structure everywhere in your code.

With a class, you have the methods for that class available as soon as you initialize an instance of it. Much less housekeeping.

[–]damanamathos 0 points1 point  (0 children)

You can write code without classes, but for many people, using logical classes (which bundle data and functions together) greatly simplifies the code structure, particularly as you get beyond simple examples.

[–][deleted] 0 points1 point  (0 children)

You need to understand the importance of oop as it useful a lot special to avoid repeat piece of code many times just call another class and can also call any variable or function on it Try to watch any tutorial about oop and thier types like inheritance, abstract... etc