ACCESS SPECIFIERS /
MODIFIERS IN JAVA:
In java we have four types of
Access Specifiers. They are:
(1) private
(2) default (not a keyword)
(3) protected
(4) public
·
Whenever we want
to access the data from one class to another class or from one interface to
another interface, or from one package to another package, we must use the
concept of Access Specifiers.
·
Access Specifier
makes us to understand how to access the data within the package (Class to
Class, Interface to Interface, Interface(s) to Class and across the package
(Class to Class, Interface to Interface, Interface(s) to Class).
·
Syntax to declare
the variable along with Access Specifiers:
[access
specifier] [static] [final] <data-type>
<variable name> = <value>;
·
Access Specifier
represents either private or public or protected. If no Access Specifiers is
used by-default JVM will take it as default.
·
Protection Matrix
of Access specifiers:
The diagram shows the scope
of Access specifiers in different classes and conditions.
Sample Program to illustrate the data accessibility within the package:
package samePack;
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.print(n_pri+", ");
System.out.print(n_def+", ");
System.out.print(n_pro+", ");
System.out.println(n_pub);
}
}
//---------------------------------------------------------------------------------------
package samePack;
public class SDC extends
samePack.SBC
{
public SDC()
{
//System.out.print(n_pri+", "); Not accessible
due to private.
System.out.print(n_def+", ");
System.out.print(n_pro+", ");
System.out.println(n_pub);
}
}
//---------------------------------------------------------------------------------------
package samePack;
public class SInd
{
samePack.SBC o1=new samePack.SBC();
public SInd()
{
//System.out.print(o1.n_pri+", "); Not
accessible due to private.
System.out.print(o1.n_def+", ");
System.out.print(o1.n_pro+", ");
System.out.println(o1.n_pub);
}
}
//---------------------------------------------------------------------------------------
public class SPDemo
{
public static void main(String aa[])
{
System.out.println("With respect to SBC : ");
samePack.SBC o1=new samePack.SBC();
System.out.println("With respect to SDC : ");
samePack.SDC o2=new samePack.SDC();
System.out.println("With respect to SInd : ");
samePack.SInd o3=new samePack.SInd();
}
}
Sample Program to illustrate
the data accessibility across the packages:
package otherPack;
public class ODC extends
samePack.SBC
{
public ODC()
{
//System.out.print(n_pri+", ");
//System.out.print(n_def+", ");
System.out.print(n_pro+", ");
System.out.println(n_pub);
}
}
//---------------------------------------------------------------------------------------
package otherPack;
public class OInd
{
samePack.SBC o1=new samePack.SBC();
public OInd()
{
//System.out.print(o1.n_pri+", ");
//System.out.print(o1.n_def+", ");
//System.out.print(o1.n_pro+", ");
System.out.println(o1.n_pub);
}
}
//---------------------------------------------------------------------------------------
import otherPack.*;
class OPDemo
{
public static void main(String aa[])
{
System.out.println("With respect to ODC : ");
otherPack.ODC o1=new otherPack.ODC();
System.out.println("With respect to OInd : ");
otherPack.OInd o2=new otherPack.OInd();
}
}
//---------------------------------------------------------------------------------------
No comments:
Post a Comment