Understand Classes, Instances, attributes, methods and Objects, in
A language is a key in any communication system; understanding each other establishes a good channel of exchange within two or more entities starting from mankind to computers.
You deciding to be a programmer implies that you must know some programming concept and jargons.
In this tutorial I will be explaining to you what classes, instances and objects are when it comes to oriented object programming. I assume that the understanding of these terminologies will be a very important advantage for those who are having problems of getting the OOP very well.
For simplicity purpose, I will be using PHP to illustrate each example I give in this tutorial, but notice that you can easily translate these examples in other programming languages.
Since I love real life examples, here I will be using the case of a cement block Molder to illustrate this concept.
Before let us understand what a molder is. According to wikipedia, it is :
â€œ A frame or model around or on which something is formed or shaped.â€
So, from this definition we can see that a mold in an object with a precised form that helps to produce many other objects with the same form as shown in the following image:
The Block Molder is meant to produce blocks. It is created with some settings/characteristics such as with, height, profundity (depth), handler and it is awarded with an ability which is to produce blocks.
Before you think of creating blocks, you have to prepare your molder and all settings it requires which will be as your template or model, analogically, the molder is called a class in oriented object programming.
A class is a model or template code that helps you to repeat an action every time you need to do it without recreating/repeating that particular portion of code. As a model (template), we have to prepare our class with all its settings and abilities.
So we can decide for instance that our class will help us create blocks with following characteristics:
Width : 140 cm
Height : 40 cm
Depth : 15 cm
These characteristics are called attributes, properties or member variables.
It depicts an aspect of an object. It helps to define the state of that object. In a class, Attributes are simply represented by variables with a specific way of declaring them.
And as ability, it will define our blocks for us.
Allow me to repeat myself by quoting this sentence a said previously, â€œSo we can decide for instance that our class will help us create blocksâ€¦â€
In this sentence, we can notice that our class has an action to perform which is â€œcreateâ€. That is called a function or a method or member function.
They are sub-logics under a class, which means that they perform a particular action and this action can easily be performed by anyone who uses your model/class. It also defines actions that all product that comes out of the model can perform or have as a behavior.
I have just mentioned â€œproductâ€ now in the definition of a method. If you are following carefully we could know that those products are â€œobjectsâ€ produced from our molder or model (class).
Objects are simply what you can create out of your molder or model (class). They have all the characteristics defined in the model.
Finally, we have gotten to it. This concept is very tricky especially for beginners (like me x:D).
Imagine we are going to use or molder to create some blocks. Surely we will not just leave it and it will do everything by itself. So at least we have to fill it out with some mixture of cement and water.
This action is called instantiation; it is a kind of initiating the process of creating an object. So before getting blocks, you must fill out the molder with cement, analogically this is call instantiating a class in oriented object programming.
So, if all terms are clear to everyone, let us get our hands dirty with some code to show how it looks like in programming. Remember I said I will be using PHP here for my examples.
Class: a class is created by using the key word â€œclassâ€ followed by the name we want to give to our class and we open and close curly braces after the name, like this:
Actually our class doesnâ€™t do anything, so we have to indicate its characteristics and behaviors like this.
With this code now it is like we have this:
We are now ready to cut our blocks or objects.
Remember I said that we fill out the moulder before getting the blocks and that was called instanciation. So this is how we can instanciate our class:
So our class is instantiated, now it is like we have our molder filled out with enough cement which means we are ready to produce an object. In real life all we have to do is to pull out the molder and we have our block formed. In programming, this process is just made by calling the method in charge of making that particular action, like this:
Now it is like we have done this:
So in the last code we made use of our class to produce an object. Note this is a very basic way of using a class. You can now repeat that action and have more blocks like this:
With this action you are creating many objects from one model, your class.
So you can now enter in production like here:wink
And if you want you can give more behaviors to your objects by adding them as methods in your class. You can for example add a method build().
So, that is it for now...
I have used some key words like â€œprivateâ€, â€œpublicâ€ that I did not explain, note that even if you donâ€™t use them in these examples it will still work but it is not a good practice. So I will try in my next tutorial to explain what they are and why we have to use them, and why not, how to use them.
If you have enjoyed this tutorial please share and like it. Or if you have any question, you can use the forum to ask or you can ask just under the tutorial.