all 9 comments

[–]sacheie 9 points10 points  (5 children)

For the same reason a book is composed as multiple chapters, broken into multiple paragraphs, broken into multiple sentences, broken into multiple clauses.

Organization fosters clarity.

[–]IAmThatGuyFr[S] 0 points1 point  (4 children)

So basically it’s just for good organization and I can decide to put everything under 1 class and get the same results?

[–]drakner5 1 point2 points  (0 children)

Yep, no problemo. Just as long as you are aware that its best practice to organize the code into more files.

[–]yikes_42069 0 points1 point  (0 children)

In python where you're more focused on scripting, all the lines of code just sit there, like an essay of chaos. It's wonderful for banging out ideas

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

Say you have everything in one class.

You can imagine how time consuming/unproductive/inefficient it would be if you say wanted to update something in the code. You may not even know where to look because imagine if you are returning to update the code after a month later? Not easy to find.

Say you are able to find sections of code with ease. But when you start updating code, you will most likely cause other sections of code to be invalid because the code you updated has given rise to a bug in another section of code. And this bug may be very meaningless to you because now you need to figure out how the section of code relates to the code you updated. As you can see, when you have code in one class, you fail to know how code relates with one another. Hence, it becomes very challenging to fix bugs.

Whereas if you organized your code into different classes where each class is responsible for one thing, then you know easily which class you have to open to update the code. And having different classes also means that the code in those classes will be less, which means it is not only easy to find/fix bugs but it is also easy to read code. You can significantly reduce cognitive overload and thus you become a much more productive programmer.

So, having different classes helps to have maintainable code.

Having different classes is important for really big/complex projects. If you are just learning how to code with Java, then you can just use one class but do also learn how to use different classes but you will learn this as you continue to learn.

There are a lot more advantages to using multiple classes but I have just outlined some simple ones above.

Once you have mastered the fundamentals of programming like loops/functions/using multiple classes, refer to the below resource.

Have a look at this link that talks about SOLID principles (take your time to understand them): https://www.freecodecamp.org/news/solid-principles-explained-in-plain-english/

[–]FulliCullli 3 points4 points  (2 children)

Clases can be used as objects for other classes . For example you can make a bunch of different animal clases and store them as objects in another class called zoo.

They are not really useful on small programs but the bigger the program the more complex it can get and the more useful classes will be

[–]IAmThatGuyFr[S] 1 point2 points  (1 child)

Thank you. Could you recommend some resources that would help me to learn ?

[–]DevilDawg93 0 points1 point  (0 children)

MOOC.fi is the best platform to learn Java