Access Levels In OOP | Access Levels

Intro To Access Levels

As I said yesterday in the post on classes , I would be going over the meaning of “Public” , “Private” , “Protected” which are either called access levels or access modifiers or whatever it is called when you read this post. Their main purpose to specify how they can be used by other objects during execution. They are used when we define a variable or a method , such as

public int gear;
.
.
.
public void setGear(int newValue) {
        gear = newValue;
}
.
.
.
.

We shall start with the most obvious of all , “Public” , which you might have guessed , means free for all. Basically , you can use a public method or a public variable anywhere , within classes or packages. That makes sense isn’t it? When you write an employee class , and your partner is writing a department class , you expect he would use some of your employee class variables in his class. Then why not make public the default? Instead of writing a method to set gear , look above for the code , why don’t we all write like this?

b1.gear = 6;

So what then is the difference between these two lines?

b1.gear = 6;
b1.setGear(6);

As per the code above , there isn’t any. You just assigned a value to the attribute of an instance of a Bicycle class. No big deal. But there is a big deal if you want checks and rules. What if a user assigned the gear as this ,

b1.gear = 1000;

Nothing wrong with it. It is still an integer value assigned to an integer-type variable. But there is something wrong with a bicycle with 1000 gears. From the Yahoo Answers , it seems the most gears possible on a bicycle is 24. So how would you ensure that the value of “gear” is no more than 24? You can’t if the variable is public.

But you could prevent others from accessing the variable directly it , or encapsulate it as OOP textbooks would say , by using “Protect” or “Private” access levels instead of “Public. I have not going through sub-classes so lets just take it that the main difference between “Protect” and “Private” access levels is that “Protect” allows the classes in the same package to access but “Private” variable or method is accessible only in the class in which it is defined.

Reworking Of Our Class

private int gear;
.
.
.
public void setGear(int newValue) {
        gear = newValue;
}
.
.
.
.

Changing Access Levels

Now you could no longer access the gear variable directly from outside as before. The method setGear() must be used in order to change its value. You could now place necessary codes in the function to check if the input value is 24 or less or else reject it. It would look like this.

public int setGear(int newValue) {
       if ( newValue <= 24 ) {
       gear = newValue;
       return 0;
       }
       else {
        return 1;
        }
}

Basically , if the input is less than or equal to 24 , the value would be updated and number 0 would be returned and otherwise , 1 would be returned. Of course there are much better and efficient way of checking but for our example , this would suffice.

Concluding Access Levels

Hope this clear up doubts you may have about the access levels. If you have questions , pls feel free to comment below. Cya!

Beginning OOP Classes | OOP | Class

OOP java Class Example

For the example program on Object-Oriented Programming (OOP) , I will be using Java instead of the usual suspects such as C# or PHP. The syntax of the class will be similar and so are most of the terms I would be using in this post. Here is the code from Oracle website itself , to ensure all the necessary steps are correct.

public class Bicycle {

    // the Bicycle class has three fields or attributes
    public int cadence;
    public int gear;
    public int speed;

    // the Bicycle class has one constructor
    public Bicycle(int startCadence, int startSpeed, int startGear) {
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;
    }

    // the Bicycle class has four methods or functions
    public void setCadence(int newValue) {
        cadence = newValue;
    }

    public void setGear(int newValue) {
        gear = newValue;
    }

    public void applyBrake(int decrement) {
        speed -= decrement;
    }

    public void speedUp(int increment) {
        speed += increment;
    }

}

Lets take a look at it before we modify it our own liking. For now , pls ignore the word “public” , which defines the permission for this class , eg.who can access this. We will be going through “public” , “private” , “protected” in future posts. For now just take it that “public” means its open to all , which could be good or bad thing as we shall see.

So we are talking about a type called “Bicycle” , which has 3 attributes or fields to describe it , cadence , gear and speed. Besides that , it can also do a few functions , such as “SpedUp” and so on. I think by now you should be familiar with functions , or at least be able to read it. If not , pls read my previous post one more time here.

OOP Terms : Class vs Instance

Here I would like to pause for a while to take a brief look at the differences between “Class” vs “Instance” of a class in term of OOP. Bicycle , which we have defined above , is a class , so is a man or a woman. But every bicycles are different in their own ways and similarly each man and woman are also different. You and me are both human , to be more precise “Homo Sapien” , but clearly there are differences enough to tell us apart from one another. So how do we create , or instantiate , a new bicycle?

Syntax For Instantiating An OOP Class

ClassName InstanceName = new ConstructorName();

Examples ,

Bicycle b1 = new Bicycle();
Bicycle b2 = new Bicycle();

Constructor is a special type of function that is designed specifically to create an instance. The keyword is “new” which creates a new instance using the constructor given. But if you look above , the constructor has 3 parameters , which means it is not the default constructor rather it has override it. Think of it like saying “She is a lady , but very find one indeed”. So she has gone beyond what is considered a lady , same for the constructor we have here. So do you tell the difference between a constructor and a normal function? A constructor has these attributes ,

  1. It does not return anything , not even void. It is just “public class-name”
  2. The name of the constructor must be same as the name of the class , including the first letter in capital.

To make a new instance of a Bicycle using the constructor given , we would have to change our code ,

Bicycle b1 = new Bicycle(10 , 15 , 6);
Bicycle b2 = new Bicycle(15 , 9 , 2);

We now have 2 new bicycles! They are both bicycles but they are not the same. Whatever you do to one doesn’t affect the other in any way. This is one of the main concepts of OOP so pls besure to understand it.

OOP Class Ends!

That is it for now but I shall be going more in-depth into this example and also on OOP theory. Cya!

Classes & OOP | Object

Historical Object

Today being the Sunday and all , I am not interested in writing anymore codes. Lets talk about a programming paradigm. OOP or Object Oriented Programming as has been available since Smalltalk-80. In fact Smalltalk , if you don’t know what is it , here is the link to wiki , is described as more object-oriented than C++/Java. Don’t worry , we won’t be going into details on why it is more OOP or why it is not. Take this as a more of historical value than any thing else.

So why is OOP so popular and why should you use it? Well , if you are writing a program to display some values on the web or on desktop, there isn’t much different between any of the programs. You would likely be using keywords such as as “echo” , “printf” , “Console.WriteLine” or any other variations , depending on your language. But if you are designing a software , how you look at it becomes as important or even more important than writing codes.

Bedtime Object Stories

To illustrate , to the best of my abilities , lets do a story on object. When we were talking about functions and how they work , I made up a story about cooking a meal. Lets do the same thing for OOP as well.

A Person As An Object

We can think of a person as an object and what we do actually. What do you do when you see a house burning? You would call a fire-station expecting fire-man to put down the flames. When you are sick , you would go to a doctor. So instead saying

FireAlert(NewYork);
CallDoc(Flu);

You would think of it as ,

FireMan.Rescue(NewYork);
Doc.Cure(Flu)

So instead of writing a block of codes that does something , you are saying that this block of code is an ability of a function of an object. In fact , that what we been doing all along. “System.Console.WriteLine” is about an class called “System” that has a sub-class “Console” which has a method or a function called “WriteLine”.

So in most textbooks , this way of classes with sub-classes would be called “Grandfather-Father-Child” example. In our example , we could put our firemen and doctors as

Person.Fireman
Person.Doctor

And of course , both firemen and doctors would have similar attributes and abilities such as

Person.Name
Person.Address
Person.Age
Person.Sex
.
.
.
Person.run
Person.walk
Person.swim
.
.
.

But there would also be something that firemen could perform but doctors could not and vice versa

Person.Fireman.Rescue
Person.Doctor.Cure

And somethings both won’t be able to do or have

Person.Fly
Person.ThirdLeg

To Conclude A Lesson In Object & Classes

If you didn’t get any of my words , don’t despair! IMHO , OOP is much more clearer if you are coding. Thats what we be doing for next few days so be sure to come back and check the site! Cheers!!