![]() |
|
[Java] Java - Object Oriented Programming - Mini Tutorial 1 - 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] Java - Object Oriented Programming - Mini Tutorial 1 (/showthread.php?tid=4772617) |
Java - Object Oriented Programming - Mini Tutorial 1 - United States of America - Apr 10, 2015 Mini Tutorial 1 - Classes, Objects, and Encapsulation In case you were wondering what exactly this thread was about: Below I am going to cover some OOP Concepts in Java. Every few nights or so I will cover a few OOP Concepts in Java in very miniature tutorials. I hope everyone enjoys. Classes Okay by now you're probably saying why is he defining class? We make a new file, write the code inside, boom. Class. However we can think of classes as a bit more than just a class. Classes represent a blueprint or sort of a layout that contains all of your fields and methods. How can I use a class inside of another class? This is where objects come into play, however we won't talk too much about objects in this paragraph. So what goes inside of these classes of ours? Your code. As stated above earlier your class contains all of your fields and methods. Inside of classes you can declare variable, methods, and the actions that these methods will produce when they are called. There is one method inside of your class that we consider special. This method is called your constructor. Whether or not you create a constructor for your class Java will call it. Also, these "constructors" are used to create a new object. Constructors can perform any action specified by the user, however they are there to initialize the data fields that you may have created. Objects There is something very important about objects. Two things that objects are made of are attributes and methods. What is an attribute(State of an Object)? Attributes are the characteristics that define an object. They matter because attributes are what give objects their "uniqueness." Let's look at an example like a car. When you think of a car a couple of things you might want to consider are color, cost, model, and manufacturer. So if we were to create a Dodge and Jeep object we can distinguish between both of them based on how unique they are from each other. What is a method? -Simple concept for now. Might add back in later if anyone wants.- So back to the question, How can I use a class inside of another class? We simple have to create an instancte(or object) of the class inside of the other class. Example below: Code Class className = new Class();Encapsulation Encapsulation deals with packing all of our data(instance variables) and storing them all inside of a single Class. We also want to include that is refers to hiding your data inside of an object. Why use Encapsulation? We use encapsulation to keep are data in our classes safe so that not just anything can come and change the data. How else are we supposed to get and change data? To modify data we use a set of things called Mutators and Accessors better known as setters and getters. Also, notice these methods use the public access modifier. Example of get and set methods. Code int x = 0;I hope you enjoyed reading, if you did read, the mini tutorial. As I said above I will continue covering OOP and other concepts in Java. Thanks. * - nOrr - Apr 10, 2015 Write something we haven't seen before like the SOLID principles and examples for each principle (before and after refactoring for example). * - WildeThing - Apr 11, 2015 (Apr 10, 2015 11:49 PM)nOrr Wrote: Write something we haven't seen before like the SOLID principles and examples for each principle (before and after refactoring for example). That would be interesting. I find the SOLID principles kinda vague so I prefer to stick to MVC SoC practices. * - United States of America - Apr 11, 2015 I think I'll probably do that next to be honest. * - nOrr - Apr 11, 2015 (Apr 11, 2015 03:09 AM)WildeThing Wrote: That would be interesting. I find the SOLID principles kinda vague so I prefer to stick to MVC SoC practices. True, the overview on Wikipedia is pretty vague, but if you read about each principle, it's pretty well described. Most of the principles are described in greater details in Uncle Bob's book "Clean Code". MVC is nice, but if your code is messed up and ugly, MVC won't do much for you except separate the logic (which is really nice, though). :) |