Monday, 15 May 2017

Tutorial 6: Access Specifiers: public, private, protected, final. What Do These Mean?



You’ve definitely noted that I’ve been using the keyword public in the past few tutorials. In the Person class for example from the last tutorial, you notice that the first line reads:

            public class Person {

Within the body of the Person class, you find the main method that goes like this:

            public static void main (String[] args) {

Now, what do we mean by using the keyword public? And what about the keywords private and protected? Let me explain.

In any relationship, there have to be boundaries. These boundaries have to be respected by the people in that relationship. If one or both of the parties in a relationship disrespect these boundaries, then there will be problems and the outcomes might be catastrophic.

When you are writing a program in Java or any other language, you do so because this program will be used by another programmer or another person. However, there are certain parts of your program that you would like the user to have access to and other parts that you would like to restrict access. Why would you like to restrict access to some parts and not others?

Here is the reason. Let’s say that you have created a program and released the first version for public use. You study the way it is used, you get user feedback and usage statistics and you discover that by making certain changes in your code, you will improve the user experience of your program. But by the time you make these improvements, your users might already have used your software and created their own programs. 

By limiting access to certain parts of your program, you ensure that you can make future improvements to your software without affecting the people who are already using your program. If everything is left open meaning that the user can modify everything in your program, then making changes to your program will render whatever they have created for themselves using your program useless.

Rules of Thumb

I’d like to ask you to read this section very carefully, because from now on, we will make changes to the way we have been writing code in the previous tutorials.

By convention, the properties of a class are defined as private.

By convention, the methods within a class are defined as public.

By convention, constants or values that you never want changed are defined as final.

Private

When we declare something private, we mean that it cannot be accessed outside the curly brackets of the class in which it is defined. What do I mean?

public class Person {// This is the opening brace of the class

            private int age;

            private double height;

            private double weight;

} //This is the closing brace of the class

You can access and even change the values of age, height and weight as long as you are within the opening and closing curly braces of this class.

So, Java allows you to do the following:

public class Person {// This is the opening brace of the class

            private int age;

            private double height;

            private double weight;

           

            public static void main(String[] args) {

                        Person brian = new Person();

                        brian.age = 25;

}

}//This is the closing brace of the class

However, you cannot do this:

public class Cat {// This is another class Cat

            private int age;

            private double height;

            private double weight;



            public static void main(String[] args) {

                        Person brian = new Person();

                        brian.age = 25;   
/*You cannot access a private property of a class directly from another class*/

}

}

So simply put, you can only define private member variables within the opening and closing curly brace of the class in which they are declared. However, you can use public methods known as getters and setters to access these values.

No comments:

Post a Comment