r/learnjava • • Oct 30 '26

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?

12 Upvotes

16 comments sorted by

View all comments

1

u/yel50 Nov 02 '26

the main opposing paradigm is FP, which preaches the separation of code and data. however, that separation is impossible. you cannot write functions that work with whatever data you hand them. functions always make assumptions about their input. they cannot be separated from their data.

for example, if you have a function to add to a list,

fn add(list, item)

there is nothing stopping you from calling it with a hash table, integer, or whatever else instead of a list. that add function requires a list be the first argument. classes make it impossible to pass the wrong type as the first argument.

list.add(item)

now, the add method cannot be handed a hash table or something by accident. even in fully dynamic languages, like js or python, classes give a certain amount of type safety.

in short, classes are a data type combined with the functions that operate on that type. or, in FP terms, they're partial function application applied to a group of functions instead of only one.

abstract methods, which include interfaces, are how continuations are done in OOP.

in good OOP design, each class has one job, holds the state for one type of data, and only includes the methods that are needed to operate on that data.