The Best Technical Blog Ever

twitterrss

Java Access Modifiers

Modifiers are Java keywords that provide information to compiler about the nature of the code, data, classes and access levels. There are two types of modifiers in java:


  • access modifiers
  • non-access modifiers

Access Modifiers :

Java provides 4 types of access modifiers to specify the accessibility (scope) of classes, datamembers, methods and constructors. They are:


  • private- visible to the class only.

  • default - no modifiers needed. visible to the package.

  • protected - visible to the package & all sub classes.

  • public -visible to the world (all the classes).

1.private access modifier:

The private access modifier is accessible only within class. Methods, Variables and Constructors that are declared private can only be accessed within the declared own class itself. It is the most restrictive access level modifier. The main usage the private modifier is hiding the data from the outside world.


If a method or variable is marked as private, then only code inside the same class can access the variable, or call the method. Code inside subclasses or external classes cannot access the variable or method. Private modifier is mostly used for fields, constructors and methods. Because if a class is marked as private then external classes cannot access that class. Classes and interfaces cannot be private.



Example1:

private

Here Test class contains private data member and private method. We are accessing these private members from outside the class, so it shows compile time error.


Example2:

private

Here, other classes cannot retrieve or set private variable's (name) value directly. So, to make this variable available to the outside world, we defined two public methods: getName(), which returns the value of name, and setName(String), which sets its value.

If the constructor of a class is private, you cannot create the instance of that class from outside the class. It reports compilation error.


Example3:
private

2.default access modifier (no keyword used):

If no access modifier is specified, by default it is treated as default modifier. Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc. The default modifier is accessible only within package. All classes in the same package can access the feature. A variable or method declared without any access control modifier is available to any other class in the same package


Example:
default

default

Here, We are accessing the Test class from outside of its package, since Test class is default, so it cannot be accessed from outside the package.


3.protected access modifier:

The protected access modifier is accessible within package (all classes) and sub classes of other package through inheritance. The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class. But a subclass in another package can access the protected members in the super-class via only the references of subclass or its subclasses. A subclass in the same package doesn't have this restriction. It ensures that classes from other packages are accessing only the members that are part of their inheritance hierarchy. Protected access gives a chance for subclass to use the helper method or variable, while preventing a nonrelated class from trying to use it.


Example:
protected

protected

Here Test class of p1 package is public, so it can be accessed from outside package. But print method of this package is declared as protected, so it can be accessed from outside the class only through inheritance.


4.Public access modifier:

The public access modifier is accessible from everywhere. A variable or method that is public means that any class can access it. public access modifier is useful when the variable should be accessible by your entire application. Usually common methods and variables that need to be shared everywhere are declared public. It has the widest scope among all other modifiers. However if the public class trying to access is in a different package, then the public class still need to be imported. In case of class inheritance, all public methods and variables of a class are inherited by its subclasses.


Example:
public

public

Java access modifier's access level table :

Access Modifier Accessibility within class Accessibility within package Accessibility in other package by only subclass Accessibility in other package
private Yes No No No
default Yes Yes No No
protected Yes Yes Yes No
public Yes Yes Yes Yes

Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status