r/learnprogramming • • 27d ago

BEST WAY TO IMPLEMENT OOP

I've been learning Java for about a month now. So far I've covered OOP principles, Java Collections, and I'm planning to dive deeper into exception handling next.

However, one area where I still struggle is OOP design and structuring. I understand concepts like inheritance, encapsulation, abstraction, and polymorphism, but when it comes to applying them in actual projects, I often find myself struggling to distribute them properly among classes.

No matter how many classes I create to separate different parts of the logic, I eventually end up with at least one class containing too many methods and responsibilities.

What are the best practices, concepts, or design principles I should study to improve my OOP design skills? How can I develop the ability to identify when a class is doing too much, decide when to create new classes, and structure a Java application in a clean, maintainable, and scalable way?

1 Upvotes

20 comments sorted by

2

u/Reasonable_Slide6304 27d ago

are you focusing too much on OOP principles and not just getting the program to do what you want?

1

u/SadCar1494 27d ago

Well I can get the program to work, but like I said, most of that work is done by one class. If that makes sense

1

u/marrsd 27d ago

It that a bad thing?

2

u/Infernaloneshot 24d ago

Yeah generally god classes are bad in OOP, you're likely not following SOLID principles

1

u/marrsd 24d ago

I was really asking OP. I'm curious to know why s/he thinks having everything in one class is bad. If you can identify the true nature of a problem, the solution is much easier to discover.

1

u/Reasonable_Slide6304 24d ago

I'd recommend checking out the legendary game programmer Casey Muratoris videos on YouTube, "clean code" horrible performance and Where does bad code come from.

14

u/aanzeijar 27d ago

I understand concepts like inheritance, encapsulation, abstraction, and polymorphism

You probably don't. I don't mean that as an insult, it's just that OOP was a reaction to a collection of problems back in the day, and without knowing about these problems, it is pretty hard to appreciate it - and to correctly use it.

If you have the time, I'd actually advise you to throw everything you know about OOP out of the window and write an entire program without any classes (which is pretty hard in Java, the next best thing would be to make everything static, but it's probably easier to learn Go or C).

You will get problems, and some of them are naturally solved by OOP.

1

u/Cybyss 26d ago

I wholeheartedly agree with this advice.

Don't do something just because you read somewhere that other people believe it's "good practice".

Just build things in whatever way makes the most sense to you. Go ahead and write lots of messy disorganized code full of public global variables everywhere, with no tests and no comments. Nothing teaches you why features exist better than seeing what happens when you don't use them.

(Obviously this advice only applies to students/hobbyists. If somebody is paying you to write code, then damned straight stick to "best practice" as far as you understand it).

1

u/TheStonedEdge 27d ago

Agree with the planning and design part. Also for an idea of software architecture look up MVC design pattern. The YouTube channel Telusko does a good video on this

1

u/SadCar1494 27d ago

Thank you, will do that :))

0

u/Euphoric_Fondant4685 27d ago

First thing that comes to mind is planning, to effectively implement OOP you need to plan out how you want your classes to interact. Id suggest starting small and design a UML class diagram on draw.io or something like that, see where everything goes, then implement it.

1

u/SadCar1494 27d ago

Thank you, I will try that

1

u/KarlisVisualDev 27d ago edited 27d ago

From my experience it is the right keywords for the topic. The questions, what help me to decide on the structure, are following: are all main topic keywords present in the class names? are all of the class names relevant? use case diagrams and app menus help in those decisions a lot.

1

u/Chad_Frost 27d ago

Composition and interfaces.

Use interfaces to declare what methods a class will have and achieve polymorphism, while you use composition to implement the functionality.

You can keep ideas like SOLID in mind as a guide. For example the Single responsibility principle says that each method and each class should only really have one job. This means if your class is doing lots of different things then you should break it up. The Interface segregation principle tells you that a class shouldn't implement methods that it doesn't need / can't use. So when you're making interfaces you shouldn't make one big interface that does lots of different things but instead break it up into smaller interfaces, and classes should only implement the ones they need.

1

u/Aggressive_Ad_5454 27d ago

The collection classes are by far the most important part of OOP in the world of making useful software.

If you work as a developer for half a century, you’ll use inheritance maybe ten times for real. Less, now that Interfaces exist.

But you’ll use collection classes every single day.

1

u/TapEarlyTapOften 26d ago

This talk on OOP really affected me a lot when I was first getting started trying to actually design software was this: https://youtu.be/o9pEzgHorH0?is=qLrI6tVJoTiPDHb3