Visibility control in java

  • Published on

  • View

  • Download

Embed Size (px)



2. ACCESS MODIFIERS Visibility modifiers also known as access modifiers can beapplied to the instance variables and methods within aclass. Java provides the following access modifiers Public Friendly/Package (default)Private Protected 3. PUBLIC ACCESS Any variable or method is visible to the entire class inwhich it is defined. What is we want to make it visible to all classes outsidethe current class?? This is possible by simply declaring the variable as publicFRIENDLY ACCESS When no access modifier member defaults to a limitedversion of public accessibility known as friendly level ofaccess. This is also known as package access. 4. So what is the difference between the public and thefriendly access??? The difference between the public and the friendly accessis that public modifier makes the field visible in all classesregardless of their package while the friendly access makethem visible only within the current package and notwithin the other packages. 5. PROTECTED ACCESS The visibility level of the protected field lies between theprivate and the package access. That is the protected access makes the field visible notonly to all classes and subclasses in the same package butalso to subclasses in other package Non subclasses in their packages cannot access theprotected membersPRIVATE ACCESS Enjoys high degree of protection. They are accessible only within their own class They cannot be inherited by their subclass and hence notvisible in it. 6. AccessModifierPublic Protected Friendly Private Access LocationSame class Yes YesYesYesSubclass in samepackageYes YesYesNoOther classes in samepackageYes YesYesNoSubclasses in otherpackageYes Yes NoNoNon subclasses inother packageYes NoNoNo