Home > java > Access Specifiers in Java Example

Access Specifiers in Java Example

Access Specifiers in Java Example

Access Specifiers in Java Example


Access Specifiers in Java are those which are applied before the data members and methods. Access Modifiers are those which are applied before class and Interface.

Access Specifiers make us understand where to access the features or where not to access the features of a program. or  Access Specifiers is always provided features controlling mechanism.


Four types of Access Specifiers

  1. private

  2. protected

  3. default (not a keyword)

  4. public

if we are not writing either private, protected or public then JVM is by default treated as “default” access specifier.


Access Specifier protection matrix

Access Specifiers in Java example

  • private access specifier is also called as native access specifier.
  • default access specifier is also called as package access specifier.
  • protected access specifier is also called as inherited access specifier
  • public access specifier is also called as universal access specifier.

Access Specifiers in Java Example

In below Example , We have four classes (SBC.java , SDC.java , SINDC.java and Main.java) which inside the ‘sp package’.By the help of below example we understand the controlling mechanism of access specifier.

//SBC.java
package sp; 
public class SBC
{
private int a=10;
int b=20;
protected int c=30;
public int d=40;
public SBC()
{
System.out.println("Same Package BASE class");
System.out.println("Private value="+a);
System.out.println("default value="+b);
System.out.println("Protected value="+c);
System.out.println("public value="+d);
}
}

//SDC.java
package sp; 
public class SDC extends SBC
{
public SDC()
{
System.out.println("Same Package DERIVED class");
//System.out.println("Private value:"+a);-->private value
System.out.println("default value="+b);
System.out.println("Protected value="+c);
System.out.println("public value="+d);
}
}

//SINDC.java
package sp; 
public class SINDC
{
public SINDC()
{
System.out.println("Same Package Independent class");
SBC c1=new SBC();
//System.out.println("Private value:"+a);-->private value
System.out.println("default value="+c1.b);
System.out.println("Protected value="+c1.c);
System.out.println("public value="+c1.d);
}
}

//Main.java
package sp;
public class Main
{
public static void main(String args[])
{
System.out.println("With respect to BASE CLASS");
SBC sbc=new SBC();
System.out.println("With respect to DERIVED CLASS");
SDC sdc=new SDC();
System.out.println("With respect to IND CLASS");
SINDC indc=new SINDC();
}
}

Lets Compile and execute/run:

E:\java>javac -d . SBC.java

E:\java>javac -d . SDC.java

E:\java>javac -d . SINDC.java

E:\java>javac -d . Main.java

E:\java>java sp.Main


OUTPUT

With respect to BASE CLASS
Same Package BASE class
Private value=10
default value=20
Protected value=30
public value=40

With respect to DERIVED CLASS
Same Package BASE class
Private value=10
default value=20
Protected value=30
public value=40
Same Package DERIVED class
default value=20
Protected value=30
public value=40

With respect to IND CLASS
Same Package Independent class
Same Package BASE class
Private value=10
default value=20
Protected value=30
public value=40
default value=20
Protected value=30
public value=40

Related Topices :-
Admin
We are group of application developer and admin.We are providing good content of information about IT technology and services .

Leave a Reply

Your email address will not be published. Required fields are marked *