r/learnprogramming • u/DatabaseMother5141 • 15d ago
Suggestions regarding programming
I'm 18. I'm currently learning Java and I feel I'm unable to understand stuff in it, especially object oriented programming. The practice feels like I'm falling behind.
16
Upvotes
View all comments
7
u/aqua_regis 15d ago
It is completely normal to struggle and that's what learning is about.
You need more practice. Much more practice.
The problem with OOP and courses is that the tasks are generally way too small for OOP to make sense. OOP works well for larger programs with more "actors".
Think about a playing card - e.g. from a Poker deck. What characteristics does such a card have? It has a suit, it has a rank, it has a face and a back, it can be face up or face down, it can have a numeric value. All what I've said up to now are state, attributes/fields/properties of a single playing card. Now what can a such a playing card do? Basically not much. It can report its suit, its rank, its value, all provided that it is face up, and you can flip the card so that it toggles between face up and face down, it maybe even can draw itself. That's the behavior, the methods. All together would form a class. This is essentially what a class is. A generic description of the state (fields) and behavior (methods) of something.
What can we do with it? We can create objects, concrete instances of the class - like the "Queen of Hearts", or the "2 of Spades".
We can store multiple such card objects in a list, in an array, etc. This brings us to the next class - the Deck. A deck is in essence a collection of cards (again, the field). What can we do with a deck? We can shuffle it, we can deal one or more cards from it, it can tell us when it is empty (when there are no more cards in the deck) - again, the methods.
With these two classes you have the very foundation of countless card games. You will need some more, like a player hand, like a discard pile, maybe piles for the players, maybe laid out cards, etc. Yet, all these hand, piles, etc. can have a common ancestor because common to all of them is that they hold collections of card objects (in the strictest sense, even the deck could be derived from a common ancestor) - that would then build inheritance.
What if you don't want a Poker Deck? What if you want a Uno Deck? You only need to change the card type to Uno Card - and the deck works just the same as before. That's polymorphism. The deck doesn't need to know what type of card it has to work with - for the deck, they are all the same.
If you design your classes well (which isn't easy and requires some experience that you only can obtain through practice), you can reuse them in many different ways.
Another example: think about accessing data from a database. Does the part that displays the data need to know how to talk to the database to get the data? No, it doesn't. All it needs to know is how to get a record (row). It doesn't need to know whether it talks to a SQLite Database, Postgres, MySQL, etc. All it needs to know is that when it calls e.g.
getRow, it gets a structure of data (array, list, map, whatever). How that data is retrieved is the business of the intermediate layer that talks to the respective database system. Again, polymorphism.Just keep going and practicing. You will at some point begin to understand.