What access types of variables can be accessed from a class in same package?

When we do not mention any access modifier, it is called default access modifier. The scope of this modifier is limited to the package only. This means that if we have a class with the default access modifier in a package, only those classes that are in this package can access this class. No other class outside this package can access this class. Similarly, if we have a default method or data member in a class, it would not be visible in the class of another package. Lets see an example to understand this:

Default Access Modifier Example in Java

To understand this example, you must have the knowledge of packages in java.

In this example we have two classes, Test class is trying to access the default method of Addition class, since class Test belongs to a different package, this program would throw compilation error, because the scope of default modifier is limited to the same package in which it is declared.
Addition.java

package abcpackage;

public class Addition {
   /* Since we didn't mention any access modifier here, it would
    * be considered as default.
    */
   int addTwoNumbers(int a, int b){
	return a+b;
   }
}

Test.java

package xyzpackage;

/* We are importing the abcpackage
 * but still we will get error because the
 * class we are trying to use has default access
 * modifier.
 */
import abcpackage.*;
public class Test {
   public static void main(String args[]){
	Addition obj = new Addition();
        /* It will throw error because we are trying to access
         * the default method in another package
         */
	obj.addTwoNumbers(10, 21);
   }
}

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method addTwoNumbers(int, int) from the type Addition is not visible
at xyzpackage.Test.main(Test.java:12)

2. Private access modifier

The scope of private modifier is limited to the class only.

  1. Private Data members and methods are only accessible within the class
  2. Class and Interface cannot be declared as private
  3. If a class has private constructor then you cannot create the object of that class from outside of the class.

Let’s see an example to understand this:

Private access modifier example in java

This example throws compilation error because we are trying to access the private data member and method of class ABC in the class Example. The private data member and method are only accessible within the class.

class ABC{  
   private double num = 100;
   private int square(int a){
	return a*a;
   }
}  
public class Example{
   public static void main(String args[]){  
	ABC obj = new ABC();  
	System.out.println(obj.num); 
	System.out.println(obj.square(10));
   }  
}

Output:

Compile - time error

3. Protected Access Modifier

Protected data member and method are only accessible by the classes of the same package and the subclasses present in any package. You can also say that the protected access modifier is similar to default access modifier with one exception that it has visibility in sub classes.
Classes cannot be declared protected. This access modifier is generally used in a parent child relationship.

Protected access modifier example in Java

In this example the class Test which is present in another package is able to call the

package xyzpackage;

/* We are importing the abcpackage
 * but still we will get error because the
 * class we are trying to use has default access
 * modifier.
 */
import abcpackage.*;
public class Test {
   public static void main(String args[]){
	Addition obj = new Addition();
        /* It will throw error because we are trying to access
         * the default method in another package
         */
	obj.addTwoNumbers(10, 21);
   }
}
2 method, which is declared protected. This is because the Test class extends class Addition and the protected modifier allows the access of protected members in subclasses (in any packages).
Addition.java

package abcpackage;
public class Addition {

   protected int addTwoNumbers(int a, int b){
	return a+b;
   }
}

Test.java

package xyzpackage;
import abcpackage.*;
class Test extends Addition{
   public static void main(String args[]){
	Test obj = new Test();
	System.out.println(obj.addTwoNumbers(11, 22));
   }
}

Output:

33

4. Public access modifier

The members, methods and classes that are declared public can be accessed from anywhere. This modifier doesn’t put any restriction on the access.

Access levels affect you in two ways. First, when you use classes that come from another source, such as the classes in the Java platform, access levels determine which members of those classes your classes can use. Second, when you write a class, you need to decide what access level every member variable and every method in your class should have. One way of thinking about access levels is in terms of the API: access levels directly affect the public API of a class and determine which members of the class can be used by other classes. You need to put as much effort into deciding the access level for a member as you put into making other decisions about your class's API, such as naming methods.

Let's look at a collection of classes and see access levels in action. The following figure shows the four classes that comprise this example and how they are related.

What access types of variables can be accessed from a class in different package?

Protected Access Modifier - Protected Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.

Which access modifiers allow the access of variable in different classes within the same package?

The default access modifier allows a variable, method, or class to be accessible by other classes within the same package.

Which attribute can be accessed from another class in same package?

The private modifier specifies that the member can only be accessed in its own class. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

Which type of variable or method is accessible within the same package or subclasses in a different package?

If a variable is declared as protected, then it can be accessed within the same package classes and sub-class of any other packages.