r/learnprogramming • u/SadCar1494 • 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?
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?
14
u/aanzeijar 27d ago
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/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
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/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