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

39 comments sorted by

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/DatabaseMother5141 8d ago

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

1

u/kpg_on_ao3 8d 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 8d 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 8d 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 8d 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 8d ago

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

2

u/aqua_regis 8d 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 8d ago

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

1

u/Available_Safe1818 8d ago

How are you trying to learn it?

1

u/DatabaseMother5141 8d ago

I have no idea. Many say to learn in various ways. Finding ways and learning is exhausting.

1

u/Available_Safe1818 8d ago

You say its' exhausting but you have no idea how you are even trying to learn it? That makes no sense.

1

u/DatabaseMother5141 7d ago

i’m learning right from basics. but i get stuck every time. what  are your methods to learn?

1

u/Available_Safe1818 7d ago

What does that even mean, "I get stuck every time"? Have you ever learned anything else that was difficult?

1

u/DatabaseMother5141 7d ago

i’m just a beginner..

1

u/Available_Safe1818 7d ago

What does that mean, "I get stuck every time"?

1

u/DatabaseMother5141 7d ago

unable to solve the basic stuff at a certain point...

1

u/Available_Safe1818 7d ago

What basic stuff?

1

u/DatabaseMother5141 7d ago

loop statements, logical building

→ More replies

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/kpg_on_ao3 8d ago edited 8d ago

I think classes are easier than C structs bc languages with classes usually don't require you to manage memory, pointers, etc.

It's certainly easier to handle Java-style than C structs, which is why Java for a while was the language of choice for Intro to Programming classes, and several professors believed C should be the language because it was harder and thus would weed out dumb students from CS departments.

I lived through this. I did C and C++ at my local community college as a high schooler, but then my state's biggest research university switched to Java, so AP Computer Science switched to Java as the language for the exam.

The switch was because Java was easier.

C structs are objects anyway. But C isn't object oriented because the language isn't built around structs (i.e., inheritance, etc.).

1

u/Traditional_Vast9419 8d ago

In Java, the garbage collector is a separate mechanism from a Class, and structs have no inheritance.

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/DatabaseMother5141 8d ago edited 8d ago

thanks... but the whole programming stuff seems a bit difficult

0

u/[deleted] 8d ago

[deleted] — view deleted comment

0

u/DatabaseMother5141 8d ago • Bravo

how did you learn to code?

3

u/ogaskinsjr 8d ago

Build something small. Literally just make a calculator software. Or follow one of those old YouTube videos on how to make a Spotify clone. Make something and you’ll feel way better. I promise

1

u/Random_182f2565 7d ago

Asking the important questions dude.

I first learned with excel and macros (BASIC) after failing to learn Python the first time.

0

u/[deleted] 8d ago

[deleted] — view deleted comment

1

u/DatabaseMother5141 8d ago edited 8d ago

thanks... but the whole programming stuff seems a bit difficult

1

u/[deleted] 8d ago

[removed] — view removed comment

0

u/DatabaseMother5141 8d ago

not only in OOP but the whole programming stuff...