Broad Network


Access Modifiers for Java Classes

Java Object Oriented Programming Core – Part 10

Forward: In the previous part of the series I talked about access modifiers for members of a class. In this part I talk about access modifier for classes in Java.

By: Chrysanthus Date Published: 3 Sep 2012

Introduction

This is part 10 of my series, Java Object Oriented Programming Core. In the previous part of the series I talked about access modifiers for members of a class. In this part I talk about access modifier for classes in Java. Before I continue, remember, a member of a class is a property or method of the class.

Package
In simple terms, a package is a set of related classes.

Access Modifier for Classes
For a class member, you can have the public or protected or private modifier. For a class you have only the public modifier. It is typed just in front of the class declaration. The modifier "public", can be present or absent in front of a class declaration. If absent, it means the class can only be seen within its own package. If present, it means the class can be seen in its own package as well as in other packages. If absent, the situation is described as package-private.

Classes of a Package
For all the programs you have seen so far in this volume, the classes have been in a single file; and none of the classes have had the public modifier (in front of the declaration). A simple way to have a package is to have your classes, each without the public modifier in the same one single file. If you would use the the public modifier for any class, that class would have to be in its own file. The following example illustrates the use of the modifier, public:

Example
Type the following code in a separate file and save it with the name, "Calculator.java", without the quotes, in the working directory (java directory).

public class Calculator
    {
        int num1;
        int num2;

        Calculator(int ident1, int ident2)
            {//constructor method
                num1 = ident1;
                num2 = ident2;
            }

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

Type the following code and save it with the name, "MainClassPublic.java" in the working directory. This code has the main method for the above class.

class MainClassPublic
    {
        public static void main(String[] args)
            {
                Calculator obj1 = new Calculator(2,3);
                int result1 = obj1.add();
                System.out.println(result1);

            }
    }

Compile the file, MainClassPublic.java, and after a while, verifiy that the other file, Calculator.java, is automatically compiled.

Now, run (execute) the file, MainClassPublic.java, and not that the two compiled files will run connected together automatically to give the result.

I have not really demonstrated how to work with packages and how the the public modifier influences the use of classes. However, what you should remember here is that, to use the modifier, public, for a class, in a simple way, the class should be in its own file. I will talk about packages in a separate series later on in the volume.

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

Chrys
NEXT

Related Articles

Java Course

Comments

Become the Writer's Fan
Send the Writer a Message