Which access modifier makes a method available to all the classes in the same package and to all the subclasses of the class?

Improve Article

Save Article

Like Article

As the name suggests access modifiers in Java helps to restrict the scope of a class, constructor, variable, method, or data member. There are four types of access modifiers available in java: 

  1. Default – No keyword required
  2. Private
  3. Protected
  4. Public

Which access modifier makes a method available to all the classes in the same package and to all the subclasses of the class?

  • Default: When no access modifier is specified for a class, method, or data member – It is said to be having the default access modifier by default. 
    • The data members, class or methods which are not declared using any access modifiers i.e. having default access modifier are accessible only within the same package.

In this example, we will create two packages and the classes in the packages will be having the default access modifiers and we will try to access a class from one package from a class of the second package.

package p1;

class Geek

{

    void display()

    {

        System.out.println("Hello World!");

    }

}

package p2;

import p1.*;

class GeekNew

{

    public static void main(String args[])

    {

        Geeks obj = new Geek();

        obj.display();

    }

}

Output:

Compile time error
  • Private: The private access modifier is specified using the keyword private
    • The methods or data members declared as private are accessible only within the class in which they are declared.
    • Any other class of the same package will not be able to access these members.
    • Top-level classes or interfaces can not be declared as private because
      1. private means “only visible within the enclosing class”.
      2. protected means “only visible within the enclosing class and any subclasses”

Hence these modifiers in terms of application to classes, apply only to nested classes and not on top-level classes

In this example, we will create two classes A and B within the same package p1. We will declare a method in class A as private and try to access this method from class B and see the result.

package p1;

class A

{

private void display()

    {

        System.out.println("GeeksforGeeks");

    }

}

class B

{

public static void main(String args[])

    {

        A obj = new A();

        obj.display();

    }

}

Output:

error: display() has private access in A obj.display();
  • protected: The protected access modifier is specified using the keyword protected.
    • The methods or data members declared as protected are accessible within the same package or subclasses in different packages.

In this example, we will create two packages p1 and p2. Class A in p1 is made public, to access it in p2. The method display in class A is protected and class B is inherited from class A and this protected method is then accessed by creating an object of class B.

package p1;

public class A

{

protected void display()

    {

        System.out.println("GeeksforGeeks");

    }

}

package p2;

import p1.*;

class B extends A

{

public static void main(String args[])

{

    B obj = new B();

    obj.display();

}

}

Output:

GeeksforGeeks
  • public: The public access modifier is specified using the keyword public
    • The public access modifier has the widest scope among all other access modifiers.
    • Classes, methods, or data members that are declared as public are accessible from everywhere in the program. There is no restriction on the scope of public data members.

package p1;

public class A

{

public void display()

    {

        System.out.println("GeeksforGeeks");

    }

}

package p2;

import p1.*;

class B {

    public static void main(String args[])

    {

        A obj = new A();

        obj.display();

    }

}

Output:

GeeksforGeeks

Important Points:

  • If other programmers use your class, try to use the most restrictive access level that makes sense for a particular member. Use private unless you have a good reason not to.
  • Avoid public fields except for constants.

&list=PLqM7alHXFySENpNgw27MzGxLzNJuC_Kdj


The access to classes, constructors, methods and fields are regulated using access modifiers i.e. a class can control what information or data can be accessible by other classes. To take advantage of encapsulation, you should minimize access whenever possible.

Java provides a number of access modifiers to help you set the level of access you want for classes as well as the fields, methods and constructors in your classes. A member has package or default accessibility when no accessibility modifier is specified.

Access Modifiers

1. private2. protected3. default

4. public

public access modifier

Fields, methods and constructors declared public (least restrictive) within a public class are visible to any class in the Java program, whether these classes are in the same package or in another package.

private access modifier

The private (most restrictive) fields or methods cannot be used for classes and Interfaces. It also cannot be used for fields and methods within an interface. Fields, methods or constructors declared private are strictly controlled, which means they cannot be accesses by anywhere outside the enclosing class. A standard design strategy is to make all fields private and provide public getter methods for them.

protected access modifier

The protected fields or methods cannot be used for classes and Interfaces. It also cannot be used for fields and methods within an interface. Fields, methods and constructors declared protected in a superclass can be accessed only by subclasses in other packages. Classes in the same package can also access protected fields, methods and constructors as well, even if they are not a subclass of the protected member’s class.

default access modifier

Java provides a default specifier which is used when no access modifier is present. Any class, field, method or constructor that has no declared access modifier is accessible only by classes in the same package. The default modifier is not used for fields and methods within an interface.

Below is a program to demonstrate the use of public, private, protected and default access modifiers while accessing fields and methods. The output of each of these java files depict the Java access specifiers.

The first class is SubclassInSamePackage.java which is present in pckage1 package. This java file contains the Base class and a subclass within the enclosing class that belongs to the same class as shown below.

package pckage1; class BaseClass { public int x = 10; private int y = 10; protected int z = 10; int a = 10; //Implicit Default Access Modifier public int getX() { return x; } public void setX(int x) { this.x = x; } private int getY() { return y; } private void setY(int y) { this.y = y; } protected int getZ() { return z; } protected void setZ(int z) { this.z = z; } int getA() { return a; } void setA(int a) { this.a = a; } } public class SubclassInSamePackage extends BaseClass { public static void main(String args[]) { BaseClass rr = new BaseClass(); rr.z = 0; SubclassInSamePackage subClassObj = new SubclassInSamePackage(); //Access Modifiers - Public System.out.println("Value of x is : " + subClassObj.x); subClassObj.setX(20); System.out.println("Value of x is : " + subClassObj.x); //Access Modifiers - Public // If we remove the comments it would result in a compilaton // error as the fields and methods being accessed are private /* System.out.println("Value of y is : "+subClassObj.y); subClassObj.setY(20); System.out.println("Value of y is : "+subClassObj.y);*/ //Access Modifiers - Protected System.out.println("Value of z is : " + subClassObj.z); subClassObj.setZ(30); System.out.println("Value of z is : " + subClassObj.z); //Access Modifiers - Default System.out.println("Value of x is : " + subClassObj.a); subClassObj.setA(20); System.out.println("Value of x is : " + subClassObj.a); } }

Output

Value of x is : 10Value of x is : 20Value of z is : 10Value of z is : 30Value of x is : 10

Value of x is : 20

The second class is SubClassInDifferentPackage.java which is present in a different package then the first one. This java class extends First class (SubclassInSamePackage.java).

import pckage1.*; public class SubClassInDifferentPackage extends SubclassInSamePackage { public int getZZZ() { return z; } public static void main(String args[]) { SubClassInDifferentPackage subClassDiffObj = new SubClassInDifferentPackage(); SubclassInSamePackage subClassObj = new SubclassInSamePackage(); //Access specifiers - Public System.out.println("Value of x is : " + subClassObj.x); subClassObj.setX(30); System.out.println("Value of x is : " + subClassObj.x); //Access specifiers - Private // if we remove the comments it would result in a compilaton // error as the fields and methods being accessed are private /* System.out.println("Value of y is : "+subClassObj.y); subClassObj.setY(20); System.out.println("Value of y is : "+subClassObj.y);*/ //Access specifiers - Protected // If we remove the comments it would result in a compilaton // error as the fields and methods being accessed are protected. /* System.out.println("Value of z is : "+subClassObj.z); subClassObj.setZ(30); System.out.println("Value of z is : "+subClassObj.z);*/ System.out.println("Value of z is : " + subClassDiffObj.getZZZ()); //Access Modifiers - Default // If we remove the comments it would result in a compilaton // error as the fields and methods being accessed are default. /* System.out.println("Value of a is : "+subClassObj.a); subClassObj.setA(20); System.out.println("Value of a is : "+subClassObj.a);*/ } }

Output

Value of x is : 10Value of x is : 30

Value of z is : 10

The third class is ClassInDifferentPackage.java which is present in a different package then the first one.

import pckage1.*; public class ClassInDifferentPackage { public static void main(String args[]) { SubclassInSamePackage subClassObj = new SubclassInSamePackage(); //Access Modifiers - Public System.out.println("Value of x is : " + subClassObj.x); subClassObj.setX(30); System.out.println("Value of x is : " + subClassObj.x); //Access Modifiers - Private // If we remove the comments it would result in a compilaton // error as the fields and methods being accessed are private /* System.out.println("Value of y is : "+subClassObj.y); subClassObj.setY(20); System.out.println("Value of y is : "+subClassObj.y);*/ //Access Modifiers - Protected // If we remove the comments it would result in a compilaton // error as the fields and methods being accessed are protected. /* System.out.println("Value of z is : "+subClassObj.z); subClassObj.setZ(30); System.out.println("Value of z is : "+subClassObj.z);*/ //Access Modifiers - Default // If we remove the comments it would result in a compilaton // error as the fields and methods being accessed are default. /* System.out.println("Value of a is : "+subClassObj.a); subClassObj.setA(20); System.out.println("Value of a is : "+subClassObj.a);*/ } }

Output

Value of x is : 10
Value of x is : 30