If any members of a class (i.e. instance variables and methods of a class) are specified as 'private', then we can access the members of a class within the class itself and we can't access them outside the class:
- We can't access from the other class under the same package.
- We can't access from the classes outside the package.
Lets implement this on Eclipse IDE:
Click here to download the previous project having 'public' access specifier and replace them with 'private' access specifiers and finally run them to find out what happens:
1. Import the downloaded project into Eclipse IDE and ensure that 'Ninth Project' with two packages under it (i..e. 'ninth_package' and 'outside_package' ) are displayed as shown below:
2. Open the 'PublicMembers' class under the 'ninth_package' as shown below:
3. Replace the 'public' access specifier before the instance variable 'a' and sample( ) method with 'private' access specifiers as shown below:
4. Run the 'AccessPublicMembers' class as shown below: (i.e. we are executing a class that is under the same package of 'PublicMembers' class)
5. Click on 'Proceed' button if 'Errors in Workspace' dialog is displayed
6. Observe than an error in the output is displayed in console as shown below (The error says that instance variable 'a' and sample( ) methods are not accessible)
At this point, we've to understand that we can't access 'private' members of a class from outside the class. Even though we try to access it from other class which is under the same package, output will produce Accessing Error.
8. Click on 'Proceed' button if 'Errors in workspace' dialog is displayed
9. Observe than an error in the output is displayed in console (The error says that instance variable 'a' and sample( ) methods are not accessible)
Finally we've to understand that 'private' members of a class can't be accessed by the other class under the same package and also can't be accessed by class from a different package of the same project.
Download this project:
Click here to download the project and import into Eclipse IDE on your machine.
But we can access the 'private' members of a class, by the other members inside the same class.
Lets implement this on Eclipse IDE (Access private members of a class by the other members of the same class)
1. Create a class named 'PrivateDemo' under 'tenth_package' or 'Tenth Project' as shown below:
2. Declare an integer variable 'a' and specify it as 'private' as shown below:
3. Declare sample( ) method and declare it as private as shown below:
4. Create a public method 'accessPrivate( )' to access the private instance variable and private method of the class as shown below and save:
At this point we've to understand that private members of the class can be accessed by the other members of the same class. In this example we've accessed them using 'accessPrivate( )' method of the same class.
5. Create another class named 'AccessPrivateViaPublic' as shown below:
6. Create an object 'object1' to access the 'accessPrivate( )' method of 'PrivateDemo' class as shown below:
7. Call the 'accessPrivate( )' method using 'object1' as shown below:
8. Save and Run the 'AccessPrivateViaPublic' class as shown below:
9. Observe that the output is displayed in the console as shown below:
This proves that we can access the private members of a class using the other members of the same class.
Download this project:
Click here to download the project containing 'PrivateDemo' and 'AccessPrivateViaPublic' class files used in this post (You can download this project and import into Eclipse IDE on your machine)
Going further we're not going to discuss on 'protected' accessed specifier and few other variations of 'private' and 'default' access specifiers. As they can only be understood after working out on inheritance projects. So lets go through them later.
Please comment below to feedback or ask questions.
'Static' variables, methods and blocks will be explained in next post.
2. Open the 'PublicMembers' class under the 'ninth_package' as shown below:
6. Observe than an error in the output is displayed in console as shown below (The error says that instance variable 'a' and sample( ) methods are not accessible)
7. Now Run the 'AccessOutside' class under the 'outside_package' as shown below:
8. Click on 'Proceed' button if 'Errors in workspace' dialog is displayed
9. Observe than an error in the output is displayed in console (The error says that instance variable 'a' and sample( ) methods are not accessible)
Finally we've to understand that 'private' members of a class can't be accessed by the other class under the same package and also can't be accessed by class from a different package of the same project.
Download this project:
Click here to download the project and import into Eclipse IDE on your machine.
But we can access the 'private' members of a class, by the other members inside the same class.
Lets implement this on Eclipse IDE (Access private members of a class by the other members of the same class)
1. Create a class named 'PrivateDemo' under 'tenth_package' or 'Tenth Project' as shown below:
5. Create another class named 'AccessPrivateViaPublic' as shown below:
8. Save and Run the 'AccessPrivateViaPublic' class as shown below:
This proves that we can access the private members of a class using the other members of the same class.
Download this project:
Click here to download the project containing 'PrivateDemo' and 'AccessPrivateViaPublic' class files used in this post (You can download this project and import into Eclipse IDE on your machine)
Going further we're not going to discuss on 'protected' accessed specifier and few other variations of 'private' and 'default' access specifiers. As they can only be understood after working out on inheritance projects. So lets go through them later.
Please comment below to feedback or ask questions.
'Static' variables, methods and blocks will be explained in next post.
1 comment:
Hi Arun thank you for your efforts to educate others :).
i Have a doubt that you said that the private class variables and methods cannot be accessed from outside the class, but in your example you are able to access the 'PrivateDemo' class variables and methods from the 'AccessPrivateViaPublic' class, can you please rectify me if i misunderstood anything.
Post a Comment