The object-oriented paradigm introduces a new concept towards accessing variables using, what are called, access specifiers. All of you who had been introduced to the OOP (Object Oriented Programming) paradigm, would definitely have heard and used these access specifiers. There are, in general, 3 levels of access specifiers in OO languages: public, protected and private.
Many new learners of the object-oriented paradigm often get confused by the purpose of these access specifiers, especially when it comes to assigning access specifiers to variables. Most literature on this explain the existence of these access specifiers as providing secure access to these variables. But what kind of security are they talking about?!! They simply use the abstract term security without ever mentioning why this security is required or how this security is helpful.
Here I try to explain one of those cases where the use of these access specifiers is very handy and definitely provides, what is called, security. Please leave a comment if you know of any other scenarios where access specifiers are helpful.
Say I have a class called Person, which has a variable that stores a value for the age of a person
Case 1: If I happen to declare this variable with public scope, then I can access this variable directly using the dot(.) notation. Let assume that in my application I create an object of this class and I name it obj. Then I can read the age of a person into another vairable var by a simple statement:
Case 2: If I happen to declare this variable with private or protected scope, then I cannot access this variable in the fashion show above. I would have to use member functions of the Person class to read or write values to this age variable. These functions are called getter/setter functions in the OOP paradigm.
Lets say, I have declared 2 such functions:
Now that these 2 cases have been established, its time to decide when to use either of these cases. This is often a confusion among beginners, who generally always stick to one of the access specifiers without giving a thought to the logic and the different scenarios that their logic would have to deal with.
Here I am going to discuss one of the scenarios, in short.
Consider a system which maintains a record of everything you have done so far since the minute of your birth (Scary isn't it!? :) and in such a system, while entering some information, lets say that the age has to be specified and the form does not have validation code programmed into it. Then, if I accidentally enter 1000 (which is unthinkable right now except if you are HanCOCK), this method would set the age to 1000 without any fuss in both the cases (described above). But this is wrong! We need to validate the value for age before it is saved but there is also a restriction that this validation cannot be written in the form code. So what do you do!?
This is where the implementation of Case 2 comes into play. Here you can program the validations in the setAge(unsigned int age) function just before saving the value in the variable. So now our setAge(unsigned int age) function would look more or less like:
But this explanation should not serve as a support for tagging all variables under the private or protected access. Always remember that an assignment via a function call is always slower than a direct assignment. Use the dot(.) notation access (otherwise called public scope access) when the variable does not control any critical logic. For example, a system where a variable is required to store the latest warning or error message, among a series of messages just before the exception is thrown, can very well have the variable, that stores this error message, with public access and used with the dot notation.
And a very important thing is that these access specifiers are applicable only to variables within a class!!!
Many new learners of the object-oriented paradigm often get confused by the purpose of these access specifiers, especially when it comes to assigning access specifiers to variables. Most literature on this explain the existence of these access specifiers as providing secure access to these variables. But what kind of security are they talking about?!! They simply use the abstract term security without ever mentioning why this security is required or how this security is helpful.
Here I try to explain one of those cases where the use of these access specifiers is very handy and definitely provides, what is called, security. Please leave a comment if you know of any other scenarios where access specifiers are helpful.
Say I have a class called Person, which has a variable that stores a value for the age of a person
Now there are 2 ways to access this variable, age. One is using the dot(.) notation and the other is via some functions (called the getter/setter functions).class Person {.........................unsigned int age;..................................................};
Case 1: If I happen to declare this variable with public scope, then I can access this variable directly using the dot(.) notation. Let assume that in my application I create an object of this class and I name it obj. Then I can read the age of a person into another vairable var by a simple statement:
var = obj.age;and I can set the age to a new value using the statement:
obj.age = 20;
Case 2: If I happen to declare this variable with private or protected scope, then I cannot access this variable in the fashion show above. I would have to use member functions of the Person class to read or write values to this age variable. These functions are called getter/setter functions in the OOP paradigm.
Lets say, I have declared 2 such functions:
/* The setter function for the age member variable */
public void setAge(unsigned int age) {
this.age = age;
}
Then I can read the age of a person into another vairable var by using the statement:/* The getter function for the age member variable */public unsigned int getAge() {return (this.age);
}
var = obj.getAge();and I can set the age to a new value using the statement:
obj.setAge(20);
Now that these 2 cases have been established, its time to decide when to use either of these cases. This is often a confusion among beginners, who generally always stick to one of the access specifiers without giving a thought to the logic and the different scenarios that their logic would have to deal with.
Here I am going to discuss one of the scenarios, in short.
Consider a system which maintains a record of everything you have done so far since the minute of your birth (Scary isn't it!? :) and in such a system, while entering some information, lets say that the age has to be specified and the form does not have validation code programmed into it. Then, if I accidentally enter 1000 (which is unthinkable right now except if you are HanCOCK), this method would set the age to 1000 without any fuss in both the cases (described above). But this is wrong! We need to validate the value for age before it is saved but there is also a restriction that this validation cannot be written in the form code. So what do you do!?
This is where the implementation of Case 2 comes into play. Here you can program the validations in the setAge(unsigned int age) function just before saving the value in the variable. So now our setAge(unsigned int age) function would look more or less like:
This is just a simple validation. More complex validations can be added depending on the logic that is to be implemented. The advantage of programming the validation within the setter function is that we can club together the validations and the operation (in this case its an assignment) together without having to touch each and every piece of code where ever this assignment is made.public void setAge(unsigned int age) {if (age <= 100) {// Considering the biological maximum age of humansthis.age = age;}}
But this explanation should not serve as a support for tagging all variables under the private or protected access. Always remember that an assignment via a function call is always slower than a direct assignment. Use the dot(.) notation access (otherwise called public scope access) when the variable does not control any critical logic. For example, a system where a variable is required to store the latest warning or error message, among a series of messages just before the exception is thrown, can very well have the variable, that stores this error message, with public access and used with the dot notation.
And a very important thing is that these access specifiers are applicable only to variables within a class!!!
1 comment:
Hey Subhankar, Good Work!!! Will keep this'll keep every one on the same Page... Although It is better to use the term "Access modifiers" instead of "Access Specifiers" & do add in ur analysis on "Non-access modifiers", like Abstract/Final and few more... Will let U know if U find anything useful on this
Post a Comment