![]() |
|
[Java] [Complexity] The Basics of Objects - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] [Complexity] The Basics of Objects (/showthread.php?tid=3693036) |
[Complexity] The Basics of Objects - Pichu - Aug 14, 2013 ![]() A Short Guide About Objects
Note: Not for complete beginners. This guide assumes prior knowledge on basic Java syntax and functiomsWhat is an object? An object is an instance of a class. It can be compared to one of many buildings built using a single blueprint, the blueprint being a class and the buildings being objects. Why would I want to use an object? Oftentimes, you might find yourself needing to have multiple copies of a certain group of variables, each with different values for those variables. This can be easily done with the use of an object. A hypothetical real world example could be that you are a fisherman who needs to keep track of his/her fish's sizes and weights. You would go about achieving this goal with objects. All right, I understand what objects are, and why they are used, but I don't know how. They really aren't that confusing. They're rather straightforward in fact. First, you start out with a class. Let's go with the Fish example. PHP Code Now we have a class, but there is nothing inside the class yet. Let's add some variables, such as the fish's length and weight. PHP Code Now our fish can have a length and weigh! Yay! Now, we have to figure out how to create some "Fish" (the object). That's simple. Just add a construct. PHP Code Great, now we can make new fish by simply evoking new Fish(someInt, someInt2); Unfortunately, we will have no way of finding out what the fish's length and weight because the variables are set to private. One way you could circumvent this problem would be by making the variables public, but that is generally frowned upon. Most people create setters and getters. A typical setter and getter: PHP Code If you noticed, the construct looked kind of like the getter (the initial line did at least). It too uses public, but instead of int, it has Fish, which means that it returns a Fish (which does not actually have to be returned due to it being a construct). Now, with all of this done, you can have a Fish object, and be able to find two integers inside of it. ( IN MY WORLD LENGTHS AND WEIGHTS CAN ONLY BE INTEGERS OK. In the real world though, using floats would probably be preferable because they support decimals ) To create a new Fish object, simply do this: PHP Code To get it's length, you can call yummyFish.getLength(), and the same for weight. Thanks for reading this guide, and I hope you learned something :D I may update this in the future to contain more complicated features of objects, such as inheritance, and of course, more about Fish! * - Makaveli The Don - Aug 14, 2013 Great job. Was good to read. Thanks. š¤ * - Deal⢠- Aug 15, 2013 Good explanation, I appreciate the tutorial. * - vitafortis - Aug 15, 2013 Very short guide, but I have a few issues. 1) You didn't go into the purpose for objects except vaguely. 2) You didn't discuss Polymorphism 3) You didn't talk about default constructors or alternate constructors. Other than that, it's eh. I suggest giving more examples, show inheritance and parent objects, etc. |