r/learnjava • • Oct 24 '21

Having a hard time grasping object oriented programming

Hi guys,

So I’ve been studying Java recently and thoroughly enjoying it, I’ve covered pretty much the variables, Int, Boolean, String, Char etc. without much problem and managed to make a simple Magic Math program exercise.

I’ve now reached the part where objects and instances are introduced, but I haven’t been able to grasp it.

Is there anyway to explain in as simple a way as possible?

56 Upvotes

14 comments sorted by

49

u/Galliad93 Oct 24 '21

there is. I found it easier to imagine a factory. the factory is your program.

the factory gets blueprints. this is your class.

then the completely automatic factory creates products based on the blueprints. those are objects.

those products are not craved into the floor of the factory, they are assembled and then shiped to you.

each object is exactly the same, but once the blue print is changed, so every object created in the factory will.

if you have a factory (or class) that has the String color. and color = "black" then every car, you look at will have the color black.

Car ford = new car;
if (ford.color.equals(black)) {

//do stuff

}

But Mr. what if I want to have cars that have different colors without making new classes every time?

This is what parameters are for, we cover that next lesson. :)

7

u/[deleted] Oct 24 '21

Wonderful explanation, thank you, I will save this and keep it on hand in my next lesson.

🙏

2

u/Galliad93 Oct 25 '21

no problem. I had a hard time understanding too.

11

u/hugthemachines Oct 24 '21

I see you have gotten a nice explanation. I would just like to add. The book "Head First Java" does a pretty good job in teaching beginners about object oriented programming. You don't have to read the entire book for that so perhaps you can borrow it from the library or a friend.

4

u/[deleted] Oct 24 '21

I’ll look into it. Thank you very much.

4

u/timthetollman Oct 24 '21

A class is like a blueprint of a car. The car has things like attributes (things it is - color, model etc.) and methods (things it does - accelerate, indicate etc.)

What are you struggling with specifically.

1

u/[deleted] Oct 24 '21

I think Galliad put it in a way which is practical and understandable. Thank you!

4

u/TheSilverCube Oct 24 '21

I've not read this post but I've saved it. I gain a little more understanding with OOP the more I read. I just wanted to give my own experience that I will save and improve my knowledge of OOP any opportunity with any post about OOP. It makes a little more sense every time.

3

u/goodm1x Oct 24 '21

A class is a template, or a blueprint, that contains variables (properties) for things (objects) that you would like to create. An object is an instance of a class. So when we say that we instantiate an object, we're creating an object from the blueprint/template of a class.

Architects (programmers) draw blueprints (classes) to create houses (objects).

1

u/Roasted_Butt Nov 22 '21

I’m at a similar point in the course I am taking now. We’re starting to make our own classes and objects, and I’m having trouble understanding when to use static versus when to omit static.

3

u/[deleted] Oct 25 '21

[removed] — view removed comment

1

u/camplate Feb 13 '22

I hope we can have a conversation about this. I was going to make my own post, question, about objects but searching through previous posts this answer comes close to what I want answered. When other people compare class to animals or factories or stereo components; it still isn't a program. As I type this I am also researching and revising. Maybe this is my issue: I don't work with objects, I work with data. I don't care about Tim Smith height, weight, hair color; only that he sold 42 widgets last week and his pay is a percentage of widgets sold; which is different from John Doe.

I have 'Learn Java in 21 Days'; lesson 1 says "Procedural programs break down a problem into a series of simple steps that are followed in order", and then "OOP is a conceptualized set of objects that work together to accomplish a job." To me, the functions inside the class still need to be carried out in a certain order (procedural) to do the task. Is this any different from modular programming? I assume the advantage with OOP, as you wrote, you can create many instances of a rectangle.

For the rectangle area and perimeter example, is the area and perimeter automatically created, or only calculated/called when needed?

I also have "Head First Java", I am looking at that too.

Thank you for any help.

2

u/[deleted] Feb 13 '22 edited Feb 13 '22

[removed] — view removed comment

1

u/camplate Feb 14 '22

Yes, I'm starting to see it better; slowly working through the book(s).

I think that was exactly the problem: the book is saying everything is a class, everything is an object which may be true but also needs to be stated that they can be simple, an application that does not need other classes, to abstractions with super and sub classes.

Thank you.