Sep 2, 2019 11:02 PM
(This post was last modified: Sep 2, 2019 11:04 PM by magicparty.)
Seeing as my last programming tutorial (In the Python section) received both good feedback and some anticipation on me providing more I now bring to you a series of tutorials covering a language and paradigm that I consider myself to be somewhat of an expert with. --- At the end of this series I expect you to feel the same way!
I would also like to add that all my tutorials are off the top of my head entirely so if you see a typo or miscalculation please raise it to my attention immediately and it will be reviewed and fixed as fast as possible.
(0) Pre-requisites
Here I will list all of the knowledge you should acquire before attempting to follow through with this series.
Why use Sublime Text and not a robust IDE? --- Unless you are designing a giant collaborative project that requires serious organization there is really no reason to use more than a simple text editor with great features.
(1) Defining what an object is
What helped me understand what an Object is immediately was replacing the word class with the word object when discussing code.
Object oriented programming or OOP is a paradigm (or style) of programming that the Java language is based upon. Other languages such as C have been given an upgrade to support this paradigm which is what the ++ represents in the C++ programming language name!
Let's create a class or...object named "Orange" --- I will also give it a color and shape
This can be saved as a separate file called "Orange.java" and compiled to create "Orange.class".
We have created our first object!
(2) Instantiation
I will cover this twice --- for now just the basics, but in a later part in this series I will go over how your objects are managed via Java's garbage collector.
What is Instantiation you ask? If you have ever seen some basic Java code in the passed you may have seen something similar to this example with the Orange object we have created.
The key statement is the new statement.
When you call this statement upon your Object "new Orange();" an instance of your object and all of its parameters are moved into memory.
The first part of the statement "Orange fruitOrange =" sets the variable "fruitOrange" as a pointer to the memory address where the object is located.
(3) Construction
There is ALWAYS a constructor even if you do not assign one!
Constructors are used to execute instructions upon instantiation and can either accept argument parameters, or no arguments at all (no-arg).
(a) The No-Arg constructor
The no-arg constructor is assigned by default when the code is compiled if no other constructor is provided. This constructor executes no code instructions and assigns no variables in memory but is necessary when running the Java byte code in the JVM.
(b) Arguments constructor
This is a section of code as explained before which can execute instructions upon object instantiation. The constructor is either Public or Protected (For now we will worry only about Public) which means it can be accessed from anywhere which is necessary for when we instantiate our object from other objects.
You may absolutely have multiple constructors and this is called "Overloading" which is a term that can also be applied to methods. It is good practice to overload an objects constructor with a custom no-arg constructor (Unless the object absolutely needs to be instantiated with certain values). This custom no-arg constructor usually assigns default values to critical variables in your object.
Let's switch it up and make a "Fruit" object and give it some constructors and variables.
"this" refers to the higher scope of the object --- You will see this in my example code, it is also good practice to give argument names the same name as the variable they are to assign.
We can now instantiate our object based upon our new constructor architecture.
(4) Wrapping up the basics
Let's make a program using our new knowledge of Objects!
compile
run
Stay tuned for part two! --- This will be a fairly large series of tutorials.
I would also like to add that all my tutorials are off the top of my head entirely so if you see a typo or miscalculation please raise it to my attention immediately and it will be reviewed and fixed as fast as possible.
The basics
(0) Pre-requisites
Here I will list all of the knowledge you should acquire before attempting to follow through with this series.
- Understanding of UML diagrams Ideal Modeling & Diagramming Tool for Agile Team Collaboration
- Basic Java programming knowledge Java Tutorial
- Ability to compile and run Java programs (I will be using the Terminal and Sublime Text/vim for the entire series)
Why use Sublime Text and not a robust IDE? --- Unless you are designing a giant collaborative project that requires serious organization there is really no reason to use more than a simple text editor with great features.
(1) Defining what an object is
What helped me understand what an Object is immediately was replacing the word class with the word object when discussing code.
Object oriented programming or OOP is a paradigm (or style) of programming that the Java language is based upon. Other languages such as C have been given an upgrade to support this paradigm which is what the ++ represents in the C++ programming language name!
Let's create a class or...object named "Orange" --- I will also give it a color and shape
Code
class Orange {
public String color = "Orange";
public String shape = "Round";
}This can be saved as a separate file called "Orange.java" and compiled to create "Orange.class".
Code
javac Orange.javaWe have created our first object!
(2) Instantiation
I will cover this twice --- for now just the basics, but in a later part in this series I will go over how your objects are managed via Java's garbage collector.
What is Instantiation you ask? If you have ever seen some basic Java code in the passed you may have seen something similar to this example with the Orange object we have created.
Code
Orange fruitOrange = new Orange();The key statement is the new statement.
When you call this statement upon your Object "new Orange();" an instance of your object and all of its parameters are moved into memory.
The first part of the statement "Orange fruitOrange =" sets the variable "fruitOrange" as a pointer to the memory address where the object is located.
(3) Construction
There is ALWAYS a constructor even if you do not assign one!
Constructors are used to execute instructions upon instantiation and can either accept argument parameters, or no arguments at all (no-arg).
(a) The No-Arg constructor
The no-arg constructor is assigned by default when the code is compiled if no other constructor is provided. This constructor executes no code instructions and assigns no variables in memory but is necessary when running the Java byte code in the JVM.
(b) Arguments constructor
This is a section of code as explained before which can execute instructions upon object instantiation. The constructor is either Public or Protected (For now we will worry only about Public) which means it can be accessed from anywhere which is necessary for when we instantiate our object from other objects.
You may absolutely have multiple constructors and this is called "Overloading" which is a term that can also be applied to methods. It is good practice to overload an objects constructor with a custom no-arg constructor (Unless the object absolutely needs to be instantiated with certain values). This custom no-arg constructor usually assigns default values to critical variables in your object.
Let's switch it up and make a "Fruit" object and give it some constructors and variables.
"this" refers to the higher scope of the object --- You will see this in my example code, it is also good practice to give argument names the same name as the variable they are to assign.
Code
class Fruit {
public String type;
public String color;
public String shape;
public Fruit(String type, String shape, String color) {
this.type = type;
this.color = color;
this.shape = shape;
}
public Fruit() {
this.type = "Orange";
this.color = "Orange";
this.shape = "Round";
}
}We can now instantiate our object based upon our new constructor architecture.
Code
Fruit orange = new Fruit();
Fruit apple = new Fruit("Apple", "Red", "Semi-Round");(4) Wrapping up the basics
Let's make a program using our new knowledge of Objects!
Code
class Fruit {
public String type;
public String color;
public String shape;
public Fruit(String type, String shape, String color) {
this.type = type;
this.color = color;
this.shape = shape;
}
public Fruit() {
this.type = "Orange";
this.color = "Orange";
this.shape = "Round";
}
}
class HF {
public static void main(String[] args) {
Fruit orange = new Fruit();
Fruit apple = new Fruit("Apple", "Red", "Semi-Round");
System.out.println(orange.type);
System.out.println(apple.type);
}
}compile
Code
javac HF.javarun
Code
Orange
AppleA UML wrapping up what we have thus far:
![[Image: hf.jpg]](https://i.ibb.co/b2K89vb/hf.jpg)
![[Image: hf.jpg]](https://i.ibb.co/b2K89vb/hf.jpg)
Stay tuned for part two! --- This will be a fairly large series of tutorials.



![[Image: wEBrkxd.png]](https://i.imgur.com/wEBrkxd.png)
![[Image: vp6iypF.gif]](https://i.imgur.com/vp6iypF.gif)

![[Image: vibesignature.gif]](https://i.ibb.co/8LvxwtYQ/vibesignature.gif)