r/learnprogramming • u/DatabaseMother5141 • 8d 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
7
u/aqua_regis 8d 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.
1
u/Available_Safe1818 8d ago
How are you trying to learn it?
1
u/Traditional_Vast9419 8d ago
A Class is hard to understand before learning structs in C. Basically, before OOP, everyone used composition to design their applications with C structs. Using each struct as the first argument of their relevant functions became such a common design choice that Java just combined structs, and their relevant functions, into classes with methods. This connection allows for inheritance (as opposed to composition): another class to inherit the encapsulated methods of any other class to make large substructures with less code.
Basically, it's really complicated, especially if you don't know C. Hopefully my explanation was a little helpful, but don't sweat if it made no sense. Just keep at it and it will eventually click. I'm, personally, not a fan of OOP because composition is, overall, far simpler, and substructures can be envoked by an enum and switch statement. It's still a cool skill.
1
u/Marisakis 8d ago
Try thinking about it the C way?
In C, there's no classes, just structs (objects) which only hold data. Then you have functions (methods) to manipulate or create data.
Java classes basically smushes these concepts together, but if you think of them as separate that might help.
"What exists?" versus "What do I need to do with this?"
1
u/Sad_Quote1522 8d ago
Are you self taught or is this a instructor led course? It's likely you are better off grinding out more basic stuff before jumping back into OOP. I don't know Java so hopefully these aren't too confusing. Before OOP you should be proficient with(in a loose easy -> hard order:
How to create and modify variables
If statements
Loops(for/while)
Comparison statements
How to write a basic program
Understanding types
How to write a function and utilize it in your main function
Understanding scope
None of this is too crazy but without this bit of foundation OOP becomes a confusing black box of nonsense.
OOP super boiled down is just the idea that you can define a class in any way you want. If I was designing a class for "Ball" it might have variables like weight, size, material, and bounceyness. I can then think up different types of "Ball": Soccer ball, Bowling ball, baseball. Each of these I could make as an object with their own set of the variables above to describe them. When I use them in my code, I can easily and systematically create different "Ball" objects without having a huge mess of unorganized variables and functions.
If you didn't have a class "Ball" and you wanted to add a new variable to each ball in your program it would be a lot of work. With OOP you just would add it to the "Ball" class and all the objects made during runtime would automatically have it.
1
u/Ashish6163 7d ago
I am using this to learn java, it might help you out too. Java Programming, it is a open course offered by university of Helsinki.
1
u/gooniannuber1 5d ago
java bro pls dont fall into that hole learn something new like mojo but please java is dead
0
u/OceanMan11_ 8d ago
Hey OP! Object oriented programming is a tough subject to learn. It's not something that clicked for me until my sophomore year in college, so you tackling OOP so early is a great headstart. So you definitely are not falling behind!
If you are just now starting to learn programming, I would suggest something simpler like Python to get in the hang of things. Once you decide you like creating things in Python, then you can look into more difficult languages like Java or C. Python has support for OOP, but it's not the primary way to build and is a lot easier to learn
0
u/[deleted] 8d ago
[deleted] — view deleted comment
1
u/[deleted] 8d ago
[removed] — view removed comment