Broad Network


Class Member Access Modifiers in Java

Java Object Oriented Programming Core – Part 9

Forward: In this part of the series I explain access modifiers in Java classes.

By: Chrysanthus Date Published: 3 Sep 2012

Introduction

This is part 9 of my series, Java Object Oriented Programming Core. The reserved words, public, protected, and private are access modifiers in Java classes. In this part of the series I explain access modifiers in Java classes. If you have been reading the tutorials of this volume in the order given, them you must have seen the modifier, public; I did not explain the meaning. In this part of the series, you will see the meanings of all three and their applications.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Where to Type a Modifier
You type a modifier in front of the data type of the property or method during declaration. Before we continue, remember, a member is a property or a method of an instantiated object (or corresponding class).

What accesses the Class Members?
Members of a class can access other members (properties and methods) of the same class. Methods, operators and other classes (corresponding objects) outside the class description of a particular class can also access members of that class. An access modifier decides whether or not a method or operator or class, outside the class description can access its member inside its class description. The member an access modifier controls, is the member typed next to it in the class description (declaration).

I will use methods of classes in the illustrations of accesses to class members. I will not use operators for the illustrations.

I will be using the phrase, external method. This refers to a class method that is not a member of the class description in question. When I say an external method can access a class member, I mean the external method can use the name (variable of property or name of method) of the member as its argument or as a variable inside its declaration (including block).

The public Access Modifier
With the public access modifier, an external method can access the public members of the class. The following code illustrates this (read the explanation below):

class Calculator
    {
        public int num1;
        public int num2;

        public int add()
            {
                int sum = num1 + num2;
                return sum;
            }
    }

class Illustration
    {
        int myFn(int par)
            {
                return par;
            }
    }

class PublicAccess
    {
        public static void main(String[] args)
            {
                Calculator obj = new Calculator();
                obj.num1 = 2;
                obj.num2 = 3;
                int result = obj.add();
                System.out.println(result);

                Illustration objIllust = new Illustration();
                int myVar = objIllust.myFn(obj.num1);
                System.out.println(myVar);
            }
    }

There are two methods (classes) in the code: myFn() and main(). The first line in the main function instantiates a class object called, obj. In main, lines 2 and 3 use the properties of the class as variables. Because the class members are public, the main() method can access the members of the class, in its declaration (block). Line 4 of the main method also demonstrates this (for the add method). In statement 7 of the main method, the method, myFn() uses the property num1 of the class as its argument. It could do so because the member, num1 is public in its class.

The private Access Modifier
With the private access modifier, an external method cannot access the private members of the class. With the private modifier, only a member of a class can access the private member of the class. The following code shows how only a member of a class can access a private member of the class (read the explanation below):

class Calculator
    {
        private int num1;
        private int num2;

        public int add()
            {
                num1 = 2;
                num2 = 3;
                int sum = num1 + num2;
                return sum;
            }
    }

class PublicAccess2
    {
        public static void main(String[] args)
            {
                Calculator obj = new Calculator();
                int result = obj.add();
                System.out.println(result);
            }
    }

The class has two private members (properties) and one public member (method). In the class description, the add() method uses the names of the private members as variables (local variables). So the add() method, a member of the class, has accessed the private members of the class.

The main method declaration (second line) has been able to access the add() method of the class because the add() method is public (it has a public access modifier).

The following code will not compile because the main method tries to access (use as variable) a private member of the class:

class Calculator
    {
        private int num1;
        private int num2;

        public int add()
            {
                num1 = 0;
                num2 = 3;
                int sum = num1 + num2;
                return sum;
            }
    }

class TheNoTest
    {
        public static void main(String[] args)
            {
                Calculator obj = new Calculator();
                obj.num1 = 2;
                int result = obj.add();
                System.out.println(result);
            }
    }

The second line in the main method is wrong because at that line, main tries to access (use as variable) the private member, num1. In fact, my compiler issues the following error message:

TheNoTest.java:20: num1 has private access in Calculator
                obj.num1 = 2;
                 ^

The protected Access Modifier
If a member of a class is public, it can be accessed by an external method including a subclass. If a member of a class is private, it cannot be accessed by an external method; even a subclass cannot access it.

The question is, should a subclass not really be able to access a private member of its superclass (since the subclass and superclass are related)? Well, to solve this problem you have another access modifier called, protected. If a member of a class is protected, it can be accessed by a subclass, but it cannot be accessed by an external method. It can also be accessed by members within its class. The following code illustrates how a subclass can access a protected member of a superclass:

class Calculator
    {
        protected int num1;
        protected int num2;
    }

class ChildCalculator extends Calculator
    {
        public int add()
            {
                num1 = 2;
                num2 = 3;
                int sum = num1 + num2;
                return sum;
            }
    }

class PublicAccess3
    {
        public static void main(String[] args)
            {
                ChildCalculator childObj = new ChildCalculator();
                int result = childObj.add();
                System.out.println(result);
            }
    }

The superclass has just two properties and no method; these properties are protected. The subclass has one method and no property. Inside the subclass, the protected properties of the superclass are used as variables. Generally, when a subclass is using a member of a superclass, it is a method of the subclass that is using the member, as in this example. The above code is OK.

The following code will not compile, because line 2 in the main() method tries to access a protected member of the superclass:

class Calculator
    {
        protected int num1;
        protected int num2;
    }

class ChildCalculator extends Calculator
    {
        public int add()
            {
                num1 = 0;
                num2 = 3;
                int sum = num1 + num2;
                return sum;
            }
    }

class PublicAccess4
    {
        public static void main(String[] args)
            {
                Calculator obj = new Calculator();
                obj.num1 = 2;
                ChildCalculator childObj = new ChildCalculator();
                int result = childObj.add();
                System.out.println(result);
            }
    }

An external method cannot access a protected member of a class (superclass); however, a subclass method can access a protected member of the superclass.

Note: A member of a class can access any member of the same class independent of whether the member is public, protected or private. This would take place in the class description.

You should now know the role of the access modifiers: public, protected and private as applied in classes; the summary is:

A public member of a class is accessible by external methods and a subclass (and the class itself). A private member of a class is accessible only by other members of the class itself; it is not accessible by external methods and it is not accessible by a subclass. A protected member of a class is accessible by a subclass (and other members of the class itself); it is not accessible by external methods.

In the absence of a modifier, a member can behave like public or private, depending on the situation: I will not go into the details in this volume.

Let us stop here for this part of the series and continue in the next.

Chrys
NEXT

Related Articles

Java Course

Comments

Become the Writer's Fan
Send the Writer a Message