Monday, 28 April 2014

Access Modifiers in JAVA


1) Access modifiers helps to implement concept of encapsulation in oops.

2) Encapsulation helps to hide the data and behavior.

3) Access modifiers in java are
-> public
-> Private
-> Protected
-> default

4) Access modifiers is not applicable for local variables.

5) Access modifiers effect the accessbility in two levels
-> class
-> Members (Method and instance variable)



Access modifiers at class level

1) Public

-> when class is marked as public, it is visible to all other classes which are inside and outside

-> Its accessible to all class in other pakages, but should mention in import class.

-> There is no need import statement when a class used inside the pakage uses the class marked as public.


2) Default

-> Defult access has no keyword.

-> when there is no modifier declared, the access is defult level access.

-> The class marked with default access are accessible only to the class inside the same pakage. The outside pakage is not accessbile by the default modifier.


3) Private and protected

Private and protected is not applicable to class level declaration.



Access modifiers at member level

1) Public

-> The member with public class is access by any class inside and outside of the pakage.

-> if we want to use public member then we should import the pakage for the class.

-> If the class itself default modifier then we cannot use the public member in the class for outside the pakage. But we can use the default access modifier class have the public member inside the same pakage.



2) Default

-> The member with default access will be access to class that are in same pakage.

-> The member with default access will not be access to class that are in outside pakage.


3) Protected 

-> The member marked as protected is accessible all class in same pakage (same as default).

-> Protected members are also accessible by class outside of the pakage. But the accessing class should be a subclass of the member class.

-> Its used to acheive inheritance concept in java.


4) Private

-> When member marked as private, it is accessible only to the members inside the same class.

->  When member marked as private,other class inside and outside the pakage will not be able to access the private member of class.


No comments:

Post a Comment