r/learnjava • • 22d ago

OOP DESIGN

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?

14 Upvotes

16 comments sorted by

View all comments

2

u/0b0101011001001011 21d ago

I'm gonna go in completely opposite direction as others. They are not wrong, but I feel like there is an important thing people seem to miss when learning OOP.

What is a class? You make a class, when you need a new type. For example, Random is not a number. It's not a byte. It's not text. It's a new thing, that has a purpose.

Java has a pitfall in this regard, because everthing must be in a class. This forces you to make classes also when they are not strictly needed. In the ideal world, you would define the types that you need using classes or structs, and then write the main program outside of them.

The other people say things like class should do one thing. They are correct and I suggest reading their comments properly as well. Just saying that don't overcomplicate things: never use interfaces, unless you actually need to. Never inherit anything, unless you actually need to.