r/learnjava • u/Zappy__Boy • Oct 27 '20
Trouble Understanding Object Orientated Programming Concepts in Java
I have trouble understanding the object orientated part of object orientated programming, specifically in the JAVA language. Over the last 5 weeks I've had to learn an immense amount of coding alongside work that cover the fundamentals of CompSci and I'm struggling to retain it all.
It's been Methods have been giving me the worst headache. Before I went to university I did a little work in JavaScript which is much simpler in how it handled methods (functions) though I still found the most difficulty with them.
Java feels so much worse, now I have trouble trying to get to grips with what is a constructor and how that works, what are get and set methods and how would I properly use them.
How do I correctly pass user inputs in and out of a method. For example making a method that converts a user input to a string.
My assignments are creeping up on me and I feel useless trying to do them. I don't even have free reign to do my assignments how I want, I must use the right method/class structure so there is no avoiding methods and I feel like I need some kind of resource that explains their proper usage.
16
u/Human-Process Oct 27 '20
For me it came with time and practice, then one day I was just like hmm it’s really that simple, why didn’t I realize it before! Try to associate OOP with real world things to try and better understand.
9
u/river_robin Oct 27 '20
I felt similar to OP, and like you, one day it just clicked. Time and practice is what got me there. Just about to finish my university unit on Data structures and algorithms in Java, that’s been fun. (Ha!)
Edited to add: OP, speak to your lecturer/ tutor and ask plenty of questions, they don’t want you to fail :)
1
u/Getting_It_now Oct 28 '20
Agreed, you just have to practise and take in information from a range of resources.
15
u/desrtfx Oct 27 '20
Shortest explanation: Oracle Tutorials
In depth course: MOOC Object Oriented Programming with Java
Excellent, short tutorial videos: Derek Banas' Youtube Channel
6
u/klujer Oct 27 '20
OP the above comment is the one you most want to listen to. Derek Banas talks about general OOP concepts here.
3
u/hermitfist Oct 27 '20
If that doesn't click with you OP, keep looking up youtube videos, courses, or articles. I've recommended Derek Banas' playlist to people (uni students) new to Java and OOP and half of them think it doesn't really help sadly. They think he goes too fast and throws too many terms at once in one video. Just be persistent and never give up. You'll get it eventually OP.
11
u/Splintting Oct 27 '20
Everything in java will run inside some sort of class. Usually when studying java, a lot of learning books will use farm animals to convey examples of how OOP (Object Orientated Programming) works.
Think of an Animal. You can define an Animal with a class. It can have a name and age for example. Then you may end up with a class looking somewhat like this:
public class Animal {
private String name;
private int age;
// getters and setters omitted
}This defines a basic animal. You are then able to instantiate the animal, by calling its constructor. Every java class has a constructor. If you do not define one, an empty, no argument, constructor will be provided for you by the compiler.
The constructors purpose is to initialize variables inside the class. We could define a constructor taking name and age as parameters, since we've in our class defined those two variables. Our full class, with constructor may look something like this:
public class Animal {
private String name;
private int age;
// getters and setters omitted
public Animal (String name, int age) {
this.name = name;
this.name = name;
}
}The constructor is the class name, followed by a parameter list. A more formal description could be: access modifier className (optional parameters) { }
Following our previous example for an Animal. We are then able to initialize it by calling its constructor like so:
Animal animal = new Animal("James" 4);In regards of how to convert user input to a string. Well, that depends on the frameworks you are using and how you define them. This you need to elaborate a little more on. However, I can make an assumption. If you "simply" store the input as a String, then you should have the job done right then and there.
1
Oct 27 '20
So if I want to make a program of a farm butcher, and I want the input of some animals, can I use classes such as
public Animal (String name, int age) { this.name = name; this.name = name; } } ```
So I can put together lots of classes like these, each one being a different animal, all of them inside a butcher class to give me an output that would get their name and age, for instance, and evaluate how much meat I'd have as output depending on their name + age?
2
u/Splintting Oct 28 '20
Sorry for the late reply.
That depends on your requirements. Is all
Animalsbutcherable? Or just some of them?If we look at the latter example, we could make a Butcherable interface, which the Butcher class would work with.
A Butcherable interface could look like this:
public interface Butcherable { void kill(); }Itself does not do much. But what if we only want to butcher a subset of out animals? Cows for example, but not dogs.
Cows could then look like this (also utilizing the parent constructor):
public Cow extends Animal implements Butcherable { public Cow(String name, int age) { super(name, age); } // Then we are required to implement the butcherable interface // otherwise, we'll get an compilation error public void kill() { System.out.println("I died..."); } }This way, you've defined that a cow is butcherable, and an animal. A dog (depending on where you are from, where I am, we do not eat dogs), is not butcherable. Thus it's implementation would be somewhat the following.
public Dog extends Animal { public Dog(String name, int age) { super(name, age); } }1
u/backtickbot Oct 28 '20
Hello, Splintting. Just a quick heads up!
It seems that you have attempted to use triple backticks (```) for your codeblock/monospace text block.
This isn't universally supported on reddit, for some users your comment will look not as intended.
You can avoid this by indenting every line with 4 spaces instead.
Have a good day, Splintting.
You can opt out by replying with "backtickopt6" to this comment
5
Oct 27 '20
I read a book, "The Object Oriented Thought Process" can't remember the author, he did a fantastic job of breaking down OO concepts into understandable, down to earth ideas, and he uses Java as a reference language.
Buy the book, read it. It won't solve all your questions questions, but it will help you solve them.
1
4
u/TroubleBrewing32 Oct 27 '20
I don't even have free reign to do my assignments how I want
Firstly, this is a good thing. If you are struggling to understand constructors, getters, and setters, you do not know enough about Java to have a new and creative way to write the program. The instructor is providing you with needed structure so that you'll have the foundation needed to write more complex programs.
How do I correctly pass user inputs in and out of a method. For example making a method that converts a user input to a string.
What is your assignment in the words of your instructor? In the context of a 1st semester Java class, usually you would use methods from the Scanner object to do this rather than writing a new method to do this.
Java feels so much worse, now I have trouble trying to get to grips with what is a constructor and how that works, what are get and set methods and how would I properly use them.
Before I get into this, do you understand the distinction between a class and an object?
I feel like I need some kind of resource that explains their proper usage
Java's documentation is really robust and generally readable, once you get certain basics down.
3
u/slowfly1st Oct 27 '20
If you could post an assignment and tell me at what part you're struggling with, I could help you better.
In short:
Methods are functions, too. In OOP the difference is, that they are bound to an instance of a class and they do have access to the attributes of that instance. The names method/function are also sometimes interchangable. Sometimes you write a method which actually is a pure function (Math.abs() for instance). But in general they're the same: They have a name, input parameters and a return value.
Constructors are methods, too, just a special type of method: They need to be the same name as the class name, and the return value is implicit: A constructor in class X will return an instance of type X. You can't call them like normal methods, you have to use the new keyword.
2
1
u/frostyfauch Oct 27 '20
What are your specific if any questions? I have experience with Java and OOP in Java specifically, feel free to PM me
1
u/The_Shell_Bullet Oct 27 '20
Think of objects as entities on your program's world. For example, if you are creating a login on your application, you need a user, the user is the class User on your app. Users have, for example, a username, a password, an email an ArrayList storing his posts, and the object have exactly these parameters.
But how you create a new user? You cant hard code the data on the class. You use the construtor, saying that when you create a new user, they get the parameters given User("John", "123456", ...etc).
The methods are the actions the entity do in the context of the program, like login(), createPost(), sendMessage(), etc. They are just like the functions in JS, but functions are "free" and methods are tied to a context, User actions are methods of User, DB actions are methods of class Database, etc because of something called encapsulation.
Think of them as ways to organize your code. The things related with User will belong to its class, the database access to Database, etc
1
u/Farpafraf Oct 27 '20
This book does a pretty good job at explaining the basic concepts imho you could give it a try.
Since you did js here's some similar stuff in js the concepts are more or less the same
1
1
u/santiago0072002 Oct 28 '20
Watch Alex lee YouTube. Also I don’t know what ide you are using but eclipse can generate constructors, setters and getters with just one click :-) the dude is legit! Constructors do not have return type, methods can have a Return type, look at the method structure from examples already made, watch Caleb curry on YouTube this guys break shit down to people that got no clue about programming. I hope that helps! Also learn snippets!!! Snippets are templates basically, like in eclipse you just type sysout and ctr+tab and the whole system.out.writeline(); shows up.
1
1
u/MrGooglr Oct 27 '20
You're trying way too hard. You said you have prior experience with JavaScript. Then I assume basics of programming language is clear to you.
You're upset because you want to grab each and everything. There isn't any problem if you are not able to instantly get which method/class (or how) you should use for a problem.
Just try to do with the worst possible way you can do. That's practice that will let you select best way later.
Don't loose yourself into learning java and java only. Focus on basic logics and concepts. And the most important thing, don't loose hope.
If you need anything specific regarding java language, feel free to ask here. We all will help you out.
0
Oct 27 '20
[deleted]
3
u/AutoModerator Oct 27 '20
Please, don't recommend/use thenewboston.
They are a discouraged resource as they teach questionable practice. They don't adhere to commonly accepted standards, such as the Java Code Conventions, use horrible variable naming ("bucky" is under no circumstances a proper variable name), and in general don't teach proper practices, plus their "just do it now, I'll explain why later" approach is really bad.
Derek Banas covers about the same ground, but in much better quality.
If you're looking for an in-depth, comprehensive, high quality, free Java course, use the MOOC Object Oriented Programming with Java from the University of Helsinki and maybe Java for Complete Beginners by John Purcell as secondary resource.
I am a bot and this message was triggered by you mentioning thenewboston. Please do not respond to this comment as I will not be able to reply.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
5
u/I_Am_Hollow Oct 27 '20
If you're having trouble still, message me and I'll do my best to help.