Java Tutorial

Access Specifiers

In order to use the data from one package to another package or within the package, we have to use the concept of access specifiers. In JAVA we have four types of access specifiers. They are private, default (not a keyword), protected and public.

Access specifiers makes us to understand how to access the data within the package (class to class, interface to interface and interfaces to class) and across the package (class to class, interface to interface and interfaces to class). In other words access specifiers represent the visibility of data or accessibility of data.

Syntax for declaring a variable along with accessspecifiers:

[Access specifiers] [Static] [Final] data type v1 [=val1], v2 [=val2] ...vn [=valn];

For example:

Public static final int a=10;
Protected int d;
Int x; [If we are not using any access specifier it is by default treated as default access specifier]
Private int e;
ClassesPrivateDefaultProtectedPublic
Same package base classYesYesYesYes
Same package derived classNoYesYesYes
Same package independent classNoYesYesYes
Other package derived classNoNoYesYes
Other package independent classNoNoNoYes

Note:

  1. Private access specifier is also known as native access specifier.
  2. Default access specifier is also known as package access specifier.
  3. Protected access specifier is also known as inherited access specifier.
  4. Public access specifier is also known as universal access specifier.

Write a JAVA program which illustrates the concept of access rules?

Answer:

// Sbc.java
// javac -d . Sbc.java

package sp;
public class Sbc
{
    private int N_PRI=10;
    int N_DEF=20;
    protected int N_PRO=30; public int N_PUB=40; public Sbc()
    {
        System.out.println ("VALUE OF N_PRIVATE = "+N_PRI);
        System.out.println ("VALUE OF N_DEFAULT = "+N_DEF);
        System.out.println ("VALUE OF N_PROTECTED = "+N_PRO);
        System.out.println ("VALUE OF N_PUBLIC = "+N_PUB);
    }
};

// Sdc.java
// javac -d . Sdc.java

package sp;
public class Sdc extends Sbc //(is-a relation & within only)
{
public Sdc()
    {
//	System.out.println ("VALUE OF N_PRIVATE = "+N_PRI);
        System.out.println ("VALUE OF N_DEFAULT = "+N_DEF);
        System.out.println ("VALUE OF N_PROTECTED = "+N_PRO);
        System.out.println ("VALUE OF N_PUBLIC = "+N_PUB);
    }
};

// Sic.java
// javac -d . Sic.java

package sp;
public class Sic
{
    Sbc so=new Sbc(); // (has-a relation & within only) public Sic()
    {
//	System.out.println ("VALUE OF N_PRIVATE = "+so.N_PRI);
        System.out.println ("VALUE OF N_DEFAULT = "+so.N_DEF);
        System.out.println ("VALUE OF N_PROTECTED = "+so.N_PRO);
        System.out.println ("VALUE OF N_PUBLIC = "+so.N_PUB);
    }
};

// Odc.java
// javac -d . Odc.java
package op;
public class Odc extends sp.Sbc // (is-a relation & across)
{
    public Odc ()
    {
//	System.out.println ("VALUE OF N_PRIVATE = "+N_PRI);
//	System.out.println ("VALUE OF N_DEFAULT = "+N_DEF);
        System.out.println ("VALUE OF N_PROTECTED = "+N_PRO);
        System.out.println ("VALUE OF N_PUBLIC = "+N_PUB);
    }
};

// Oic.java
// javac -d . Oic.java

package op;
public class Oic
{
    sp.Sbc so=new sp.Sbc (); // (has-a relation & across)
    public Oic ()
    {
//	System.out.println ("VALUE OF N_PRIVATE = "+so.N_PRI);
//	System.out.println ("VALUE OF N_DEFAULT = "+so.N_DEF);
//	System.out.println ("VALUE OF N_PROTECTED = "+so.N_PRO);
        System.out.println ("VALUE OF N_PUBLIC = "+so.N_PUB);
    }
};

// ASDemo.java
// javac ASDemo.java

import sp.Sbc;
import sp.Sdc;
import sp.Sic; class ASDemo
{
    public static void main (String [] args)
    {
//      import approach

        System.out.println ("WITH RESPECT TO SAME PACKAGE BASE CLASS");
        Sbc so1=new Sbc();
        System.out.println ("WITH RESPECT TO SAME PACKAGE DERIVED CLASS");
        Sdc so2=new Sdc();
        System.out.println ("WITH RESPECT TO SAME PACKAGE INDEPENDENT CLASS");
        Sic so3=new Sic();
        
//      fully qualified name approach

        System.out.println ("WITH RESPECT TO OTHER PACKAGE DERIVED CLASS");
        op.Odc oo1=new op.Odc();
        System.out.println ("WITH RESPECT TO OTHER PACKAGE INDEPENDENT CLASS");
        op.Oic oo2=new op.Oic();
    }
};

Nameless object approach:

Sometimes there is no necessity for the JAVA programmer to create an object with some name. In such situations we can use the concept of nameless object.

For example:

// named object approach
Test t1=new Test ();
t1.display ();

To convert the above statements into nameless object approach follow the following statements.

For example:

// nameless object approach
new Test ().display ();