Hello!

I just wanted to make a post to share my excitement over discovering programming with Java. For context, my previous knowledge of any programming language barely extended into classes as I always struggled with understanding more than the basic basic things (classes? like what you wear on your face? get and set what? the table?).

I really liked the idea of programming though! So over the years (maybe 15-20?), I tried learning (and failed) with different languages, hoping something would help things click. I’ve tried C#, Basic, JavaScript, HTML/CSS and more but never Java. As to why not, I have no clue.

The reason for Java, now, is that I’ve been playing Minecraft a lot recently (my adhd thanks you Mojang) and I figured, why not give it a try? So I started a free online course covering the basics: variables, methods, objects… A week later, and after a lot of DuckDuckGoing, I now understand not only the basics but classes, IO, exceptions, and so much more! I’m currently wrapping my head around Generics but I’m having a wonderful time with it. After that, who knows :)

Take care and thank you for reading!

  • swordsmanluke@programming.dev
    link
    fedilink
    arrow-up
    1
    ·
    8 months ago

    Classes are Data plus the code required to modify that Data. The idea is to encapsulate data modifications into one thing (a Class) that knows how to modify all the Data as a single unit. This lets us write some code to describe, say, a Scrollbar widget. The Class for the widget combines all the Data for a Scrollbar (position, orientation, bar size, total size, etc) with the methods that read or modify that data (scroll up/down, change size, draw, etc).

    That’s the first Big Idea of OOP - that data should be grouped with the functions that modify it. If you don’t have that - as in C - you have to write functions that only work on a given data type but which are namespaced separately. You get functions like void set_scrollbar_pos(void* scrollbar, word pos) which become verbose in a large project. (I’m not saying this is the worst thing in the world, just a different style.)

    The second Big Idea of OOP is message passing. Now that we have code and Data bundled together, it would be nice if Objects that share functions of the same essential type and intention could be swapped out interchangeably. So instead of directly invoking a function on an Object, we send a ‘message’ that says something like ‘if you know how, please draw yourself on screen, relative to X,Y’.

    Of course, since plain English is hella verbose, the actual message is going be something like “draw, X,Y” and the Object receiving the message then sorts out if it has a method called “draw” that can use the provided X and Y. If so, it runs the code to do so. If not, you get an error.

    Messages like this mean that you can swap out compatible Classes for one another. E.g. you can ask any collection of widgets to .draw themselves with a single method and let the compiler/interpreter generate the machine code as needed. That reduces the amount of boilerplate for engineers by a lot! Otherwise, trying to work with any collection of heterogeneous Objects (like a List of every Widget contained in a Window) would need to have essentially the same code rewritten for every different Type needed - a combinatorial explosion of code!

    Tl;Dr -

    • Classes help organize code and simplify state management by combining data with the functions that manipulate that data.

    • Classes reduce the amount of boilerplate code needed by allowing methods with the same “shape” to be called interchangeably.

    Everything else about OOP is essentially built off these two ideas. I hope that helps.