3.5 Public and private modifiers

The concept

In Apex programming language, there are three access modifiers: global, public and private. Global means it can be accessed inside and outside the current package. It is generally used when we build a library or web service. We can ignore it for now if you find it hard to understand. Public means it can be accessed inside the package, but pretty much everywhere in your current org. Private means it can only be accessed within the current class.

Here is the code sample. Let’s open our Dog class and modifier it:


public class Dog
{
    public Integer age;
    public String name;
    private String gender;
    public String breed;
    public Boolean isPuppy;
    
    public void setGender(Boolean isMale)
    {
        if(isMale) 
        {
            gender = 'Male';
        }
        else 
        {
            gender = 'Female';
        }
    }

    public String getGender()
    {
        return gender;
    }
    //...
}

In you anonymous block:

Dog d = new Dog();
d.age = 20;
d.name = 'Happy';
//d.gender = 'male';
//The above line will get compile error
d.setGender(true);
d.breed = 'Golden Retriever';
d.isPuppy = false;

//System.debug(d.gender);  You can't do this
System.debug(d.getGender());

A bit explanation

We have added a bit if/else condition into the above code. We will explain more about conditional statement in next chapter. But reading it shouldn’t be too hard to understand though. If the variable isMale is set to be true, then gender will be set as ‘Male’. Otherwise the gender will be set as ‘Female’.

The concept of private modifiers is not hard to understand and remember. However, you may wonder, why do we bother to set some variables and functions to be private to reduce the convenience for us? The reason for this is encapsulation and security.

From Object-Oriented Programming perspective, a class or object should be viewed as a black box, only providing a series of interfaces to the outside word. You will understand this more when doing more programming.

However, an easier and more direct way to understand this is from security perspective. If we let the user to set the gender value, it could be any string. For example, ‘test’, ‘dust’, ‘name’, etc. Those don’t make sense as a gender. When making setGender to be the sole place the set the gender value, we secured the value to be only meaningful ‘Male’ and ‘Female’.

Ideally, we should keep all the variables to be private and only access them through methods.

Exercise

Try to set more variables in Dog class as private and add more getter and setter functions. Play with it and see the compiler error when you try to access a private variable or method from outside the class

Next Post

3.6 Use constructor to initialise

Subscribe to Sfdcinpractice

Subscribe to get the latest blogs and tutorials of sfdcinpractice. No spam, no trash, only the awesome posts from sfdcinpractice. 

Leave a Reply

Your email address will not be published / Required fields are marked *