Google+

82. 'Public' Access specifier







Using access control you can control what parts of a program can access the members of a class (i.e. instance variable and methods).

By controlling access, you can prevent misuse. For example, allowing access to data only through a well defined set of methods, you can prevent misuse of that data.

However access control to instance variables and methods of a class is determined by access specifier.

Java's access specifier's are:
  • public
  • private 
  • protected
'public' access specifier:

Example of an instance variable having 'public' access specifier:  

public int a=5;

Example of a method having 'public' access specifier:

public void Add( )
{
     System.out.println("Text inside Add( ) method");
}

When a member of a class i.e. either instance variable or method is specified as 'public', then the member can be access by any other code outside the class.

Lets implement this on Eclipse IDE:  (Accessing 'public' instance variables and methods)

1. Create a class named 'PublicMembers' under 'ninth_package' package of  'Ninth Project' project as shown below:



2. Declare an instance variable 'a' and specify it as 'public' as shown below:



3. Declare sample( ) method and specify it as 'public' as shown below and save:



4. Create a class named 'AccessPublicMembers' under the 'ninth_package' of 'Ninth Project' as shown below:



5. Create an object 'object1' to access the instance variables and methods of  'Public Members' class which are specified as 'public' as shown below:



6. Access the 'public' specified instance variable of 'PublicMembers' using 'object1' object and print:



7. Call the 'public' specified sample( )  method of 'PublicMembers' class as shown below:



8. Save and Run the 'AccessPublicMembers' class as shown below:


9. Observe that the output is displayed in the console as shown below:



From this program, we've to understand that all the instance variables and methods which are specified as 'public' can be accessed by the code outside their class. i.e. In this example we've accessed the instance variable 'a' and sample( ) method of 'PublicMembers' class which are specified as 'public' are accessed by the object 'object1' created in 'AccessPublicMembers' class.

Download this Project:

Click here to download the project containing the 'PublicMembers' and 'AccessPublicMembers' class files used in this post (You can download the project and import into Eclipse IDE on your machine)

Accessing 'public' instance variables and methods of a class  from a class which is created outside the package:

1. Create a class named 'PublicMembers' under 'ninth_package' package of  'Ninth Project' project as shown below:



2. Declare an instance variable 'a' and specify it as 'public' as shown below:


3. Declare sample( ) method and specify it as 'public' as shown below and save:



4. As we've created 'PublicMembers' class under the 'ninth_package' package, in order to access these public members from a class which is created outside the 'ninth_package' package, lets created another package 'outside_package' package under 'Ninth Project' project as shown below:



5. Create a class named 'AccessOutside' under the 'outside_package' as shown below:



6. Create an object 'obj1' to access the instance variables and methods of  'PublicMembers' class which are specified as 'public' as shown below:



7. Access the 'public' specified instance variable of 'PublicMembers' class using 'obj1' object and print:



8. Call the 'public' specified sample( )  method of 'PublicMembers' class as shown below:



9. Save and Run the 'AccessOutside' class as shown below:


10. Observe that output is displayed in console as shown below:



From this program, we've to understand that all the instance variables and methods which are specified as 'public' can be accessed by the code outside the package under which the class is created. i.e. In this example we've accessed the instance variable 'a' and sample( ) method of 'PublicMembers' class which are specified as 'public' are accessed by the object 'obj1' created in 'AccessOutside' class which is created in under another package i.e. 'outside_package' of the same project 'Ninth Project'.


Download this project:

Click here to download the project containing 'PublicMembers' and 'AccessOutside' class files used in this program (You can download the project and import into EclipseIDE on your machine)

In the next post, we are going to find out what happens when we don't specify any access specifier.( i.e. On trying to access the instance variables and methods of a class with default specifier ) and accessing the class members from outside the class and also from a class which is created under another package of the same project. 




Please comment below to feedback or ask questions.

Default Access Specifier concept will be explained in the next post.




No comments: