It seems like you are using an ad blocker. To enhance your experience and support our website, please consider:

  1. Signing in to disable ads on our site. Our users don't see ads
  2. Or, Sign up if you don't have an account yet
  3. Or, disable your adblocker by doing this:
    • Click on the adblocker icon in your browser's toolbar.
    • Select "Pause on this site" or a similar option for lancecourse.com.

Understand Classes, Instances, attributes, methods and Objects, in Object Oriented Programming

By zooboole

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 object oriented 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:

enter image description here

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.

Class

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.

Attribute/property/member variable

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.

Function/method/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).

Object

Objects are simply what you can create out of your molder or model (class). They have all the characteristics defined in the model.

Instance

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.


Practice

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:

enter image description here

Actually our class doesn’t do anything, so we have to indicate its characteristics and behaviors like this.

enter image description here

With this code now it is like we have this:

enter image description here

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:

Class instance

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:

New object

Now it is like we have done this:

Real Object

Simple right?

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:

Many class objects

With this action you are creating many objects from one model, your class.

So you can now enter in production like here:wink

enter image description here

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.

Last updated 2021-03-04 UTC