r/learnprogramming • • 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

39 comments sorted by

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.

1

u/DatabaseMother5141 15d ago

it’s almost been a year trying to understand and build the code.

1

u/kpg_on_ao3 15d ago

It's been a year? And you're 18 already? I'll try a shortcut that I haven't seen yet:

Do you have good grades in your math classes? If not, don't bother. It's not for you.

not everyone can code, and coding is a type of math, so if you aren't good at math, you won't be good at programing.

1

u/aqua_regis 15d ago

And so? Even if it is several years. Programming and OOP take time.

When I first tried to learn Java, after having been programming for way over a decade already in multiple languages, nothing initially made sense. Took me a couple tries and different learning materials before I finally understood what it is all about.

The main difference between a successful programmer and one that failed is that the former is too stubborn to give up.

1

u/ConnectAffect2346 15d ago

To be honest your explanation is very well put and it gave me a bit better understanding of oop after reading - im still a student but i am trying to use oop to make projects and the explanation you gave regarding card decks is well put and it gave me a bit more hints regarding programs i made in python using classes and programs made with javascript so i want to ask if there is let's say a plan to make a program ready to be scalable in future does it still matter if i create objects which will be used in future or is there another method to it like making a program good as is without creating objects or classes?

2

u/aqua_regis 15d ago

OOP is only one of three major programming paradigms. The other two are procedural and functional.

Programs can (and have) been written in any of all three or even in combinations of all three paradigms.

This also applies to large, scalable programs.

Certain programming languages enforce certain paradigms, and others are more free.

There is no golden rule what the best paradigm is, but there are plenty, heavily opinionated discussions about this topic.

1

u/ConnectAffect2346 15d ago

Ohh okay, so it all depends on the purpose of using that paradigma or making a combination of paradigmas?

2

u/aqua_regis 15d ago

No, it does not work like that. You can use any paradigm for almost everything. Some are better, some are worse.

Sometimes the used language defines the paradigm.

Sometimes a mix of paradigms is beneficial.

1

u/ConnectAffect2346 15d ago

Ohh okay, now i understand it seems i had the wrong perspective about paradigms, thanks for clearing it up🙂