Home > DeveloperSection > Blogs > Java Access Modifiers: The public and the private Modifiers

Java Access Modifiers: The public and the private Modifiers


Java Java  Class 
Ratings:
0 Comment(s)
 178  View(s)
Rate this:

Java Access Modifiers: The public and the private Modifiers

 The public Modifier

Previously we understand the visibility of members at certain level using access modifiers in java. Now here we see public modifier in detail. A public modifier makes the corresponding element truly public—that is, it is accessible to everybody. We would use this modifier only if we wish to make the member of a class publicly available (note that this is considered bad design and defeats the encapsulation principle of object-oriented design). Making the members visible to every other piece of code in our application makes them vulnerable to both accidental and intentional modifications, thus resulting in runtime errors in our application. We would, however, declare our classes public so that they can be instantiated by an outsider. To understand the accessibility rules of a public member, look at the program in here.

package mypackage;

                             

public class MyClass {

                     public int count;

 

                     public void setCount(int count) {

                             this.count = count;

                     }

}

 

class DerivedClass extends MyClass {

                     void someMethod() {

                             count = 10;

                             setCount(5);

                     }

}

 

class NonDerivedClass {

                     void someMethod(int count) {

                             MyClass obj = new MyClass();

                             obj.count = 10;

                             obj.setCount(5);

                     }

}

 

The class MyClass declares a public field called count and a public method called setCount. The DerivedClass, which inherits MyClass, defines a method called someMethod. The method has access to both the count and setCount members of MyClass. The NonDerivedClass does not inherit from MyClass; however, it belongs to the same package, mypackage. The someMethod within this class creates an object of MyClass and accesses its public field count and also invokes the public method setCount. Both members, as expected, are available to the code within NonDerivedClass

Now, let’s consider the accessibility of these members from the classes belonging to another package. This is shown in here.

package myanotherpackage;

 

import mypackage.MyClass;

 

class DerivedClass extends MyClass {

                     void someMethod() {

                             count = 10;

                             setCount(5);

                     }

}

 

class NonDerivedClass {

                     void someMethod(int count) {

                             MyClass obj = new MyClass();

                             obj.count = 10;

                             obj.setCount(5);

                     }

}

 

Note that we must write this code in another .java file to declare another package called myanotherpackage. We import the definition of MyClass by using the import statement to resolve its references within the current program. The DerivedClass in this new package defines a method called someMethod that, as we can see, has access to both the public members of MyClass. The NonDerivedClass creates an object of MyClass, as in the earlier case, and accesses the two public members. Therefore, as stated earlier, the identifier declared with the public attribute is truly public.

The private Modifier

The private modifier is really useful when we want to make a member accessible only to the class to which it belongs. An example of this might be a variable that stores a Social Security number or a credit card number. Consider a CreditCard class in an application that defines a field to store a credit card number. This variable should be accessible only to the code defined in the CreditCard class and not anywhere outside. We would declare this kind of field with a private modifier.

To understand the visibility of a private member, change the public keyword to private for the count and setCount declarations in the MyClass definition of the above code. If we compile the code, both files will not compile, giving us a compilation error wherever we try accessing the count and setCount members.


Don't want to miss updates? Please click the below button!

Follow MindStick