Lets discuss three different structures and a property in programming.
A class is always the highest level structure in any file.
All other structures and properties have to be within a class.
As a separate point, classes can be found in other classes as well, but again, only classes can be the highest level in a file.
The level of a structure is indicated by its indentation, see.
There are three levels in the above image.
First where there is zero space indentation.
Second where there are four spaces of indentation.
And where there are eight spaces of indentation.
The next structure is Function. Again, in the above example, there is a function.
The next structure is a constructor.
We already talked about the structure, class. Constructors are used when instantiation occurs.
When a class is instantiated, a constructor is called,(even if the programmer haven't implemented any, in which case a default constructor is called).
A constructor takes the inputs that makes this instance unique.
A constructor typically saves the inputs as variables, also performs whatever else is associated with the initialization of this object.
After the instantiation is complete, the constructor becomes irrelevant.
All instances of a class become only accessible through its functions.
An example of a class with a constructor is found below.
You can observe that above the function, there is a constructor.
There are two differences in-between a constructor and a function.
A constructor does not declare a return value, from this it also follows that it does not return anything.
The name of a constructor is the name of the class it is in.
These two are the differences between how to setup a function and a constructor.
Now, the work part of the constructor is not implemented.
To implement it, we must discuss the fourth matter in this tutorial, the property: variables.
A variable is a storage area. The value it stores can be modified and what it stores can be accessed anytime.
In the following example, a variable is created and the value of "inNumber" is assigned to the variable, now accessible across the class, including in all functions.
There are several misconceptions here that needs to be address, not now.
We will write code now.
Btw, ignore the constructor, you don't need to write it, It was just created for demonstration.
Now, run the virtual machine, open the Object-Orientation folder and open a terminal here.
Using vim, create a new file here, call it "Bike.java".
Now, develop the skeletal structure of Bike.
I want the class to support addressing the number of wheels this bike has.
We have talked about one way of addressing the value of this number. Implement it.
Now. Open Main.java.
Place two slashes before the word System.
It should turn blue.
Now, compile Main.java.
Now run Main.java.
You will notice that the "Hello world!" message is no longer present.
Placing two slashes at the start of a line, makes the compiler ignore that line. This allows creation of notes for a programmer to read, but also the "delete" of a set of lines, while being easy to recover.
Now, type the following
Bike bike = new Bike(2);
The part of the left of the equal sign is the creation of a variable.
The type of the variable is Bike.
The name of the variable is bike.
On the right side of the equal sign is the instantiation of a class.
The word new is the command to instantiate.
The word Bike is the class being instantiated.
What follows is the set of inputs the constructor will require.
And that is the end of the instantiation.
Your Main.java file should look like this.
Compile and run this code.
Again, the terminal should not display anything.
Now, create a function in bike, with the name, toString. It takes no inputs, but outputs "String"
Its return statement should look like this.
Your Bike class should look like this.
toString is part of a standard. toString is the expected name of a function whose purpose is to return a text, representing the object.
Now, return to the Main class.
On the third line, in the main function, type the command to display something in the terminal.
Now, add the input, required for the command. The input is the name of the Bike variable and the name of the function in the Bike class, returning a text representation of the Object. These two, the variable name and the function name needs to be separated by a dot. Finally, sometime in this tutorial I refer to a function by its name, but ignore the curved brackets that function names are followed by.
When you use a function, you always need to include those brackets.
Now, I ask you to compile and run this program.
The end result should be this.