Skip to main content

Introduction to Java Programming

  What is Java? Java is a programming language and a platform. Java is a high-level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Firstly, it was called  Greentalk  and the file extension was  .gt . After that, it was called  Oak . Initially Java was designed for small, embedded systems in electronic appliances like set-top boxes. Platform:  Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API(Application Programming Interface), it is called a platform. The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured, High performance, Multithreaded, Architecture neutral, Object-oriented, Interpreted, and Dynamic". Currently, Java is used in internet programming, mobile devices, g...

22. Access Modifiers in Java

 

Access Modifiers in Java

  • Access Modifiers in Java helps to restrict the scope of a class, constructor, variable, method, or data member.
  • We can change the access level of fields, constructors, methods, and class by applying the access modifier on it.
  • There are four types of access modifiers available in Java:
    1. Public
    2. Protected
    3. Default - No keyword required
    4. Private

The scope of these access modifiers is shown as:

PublicProtectedDefaultPrivate
Same classYesYesYesYes
Same package
subclass
YesYesYesNo
Same package
non-subclass
YesYesYesNo
Different package
subclass
YesYesNoNo
Different package
non-subclass
YesNoNoNo

Public

  • The public access modifier is specified using the keyword public.
  • The public access modifier has the widest scope among all other access modifiers.
  • There is no restriction on the scope of public data members.

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.

Default

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

Private

  • The private access modifier is specified using the keyword private.
  • The 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.
MyPackage

package MyPackage;

public class Modifiers {
    public int pu = 20;
    protected int pr = 21;
    int def = 1;
}

Outside MyPackage

import MyPackage.Modifiers;

class DeclaringModifiers {
    public int pub = 25;
    protected int pro = 4;
    int de = 16;
    private int pri = 6;

    public void method() {
        System.out.println("The value of public variable in same class of a package is " + pub);
        System.out.println("The value of protected variable in same class of a package is " + pro);
        System.out.println("The value of default variable in same class of a package is " + de);
        System.out.println("The value of private variable in same class of a package is " + pri);
    }
}

class Subclass1 extends DeclaringModifiers {
    public void method1() {
        System.out.println("\nThe value of public variable in subclass of same package is " + pub);
        System.out.println("The value of protected variable in subclass of same package is " + pro);
        System.out.println("The value of default variable in subclass of same package is " + de);
    }
}

class Subclass2 extends Modifiers {
    void method() {
        System.out.println("\nThe value of public variable outside package by subclass is " + pu);
        System.out.println("The value of protected variable outside package by subclass is " + pr);
    }
}

public class Main {
    public static void main(String[] arr) {
        DeclaringModifiers am = new DeclaringModifiers();
        am.method();

        Subclass1 amip = new Subclass1();
        amip.method1();

        System.out.println("\nThe value of public variable in other class of a package is " + am.pub);
        System.out.println("The value of protected variable in other class of a package is " + am.pro);
        System.out.println("The value of default variable in other class of a package is " + am.de);

        Subclass2 amop = new Subclass2();
        amop.method();

        Modifiers amo = new Modifiers();
        System.out.println("\nThe value of public variable outside the package is " + amo.pu);
    }
}

Output :

The value of public variable in same class of a package is 25
The value of protected variable in same class of a package is 4
The value of default variable in same class of a package is 16
The value of private variable in same class of a package is 6

The value of public variable in subclass of same package is 25
The value of protected variable in subclass of same package is 4
The value of default variable in subclass of same package is 16

The value of public variable in other class of a package is 25
The value of protected variable in other class of a package is 4
The value of default variable in other class of a package is 16

The value of public variable outside package by subclass is 20
The value of protected variable outside package by subclass is 21

The value of public variable outside the package is 20

Previous

Next

Comments