This post originated from an RSS feed registered with Java Buzz
by instanceof java.
Original Post: Accessibility modifiers examples
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
The keywords which define accessibility permissions are called accessibility modifiers.
Java supports four accessibility modifiers to define accessibility permissions at different levels.
Accessibility modifier keywords:
1.private
2.protected
3.public
4.default(no keyword)
1.private:
The class members which have private keyword in its creation statement are called private members. Those members are only accessible within that class.
If we declare any variable or method with private accessibility modifier then those variables and methods are accessible only within that class , not accessible out side the class.
private variable are accessible within the class :
package com.instanceofjava;
public class PrivateDemo {
private String first_name;
private String last_name;
void show(){
System.out.println("First Name:="+first_name);
System.out.println("Last Name:="+last_name);
}
public static void main(String[] args) {
PrivateDemo obj= new PrivateDemo ();
obj.first_name="James";
obj.last_name="Goosling";
obj.show();
}
}
Output:
First Name:=James
Last Name:=Goosling
private variable are not accessible out side the class :
package com.instanceofjava;
public class PrivateDemo {
private String first_name;
private String last_name;
void show(){
System.out.println("First Name:="+first_name);
System.out.println("Last Name:="+last_name);
}
public static void main(String[] args) {
PrivateDemo obj= new PrivateDemo ();
obj.first_name="James";
obj.last_name="Goosling";
}
}
package instanceofjava;
class Demo {
public static void main(String[] args){
PrivateDemo obj= new PrivateDemo ();
obj.first_name="James"; // ERROR: The field PrivateDemo.first_name is not visible
}
}
private methods are accessible within the class :
package com.instanceofjava;
public class PrivateDemo {
private String first_name;
private String last_name;
private void show(){
System.out.println("First Name:="+first_name);
System.out.println("Last Name:="+last_name);
}
public static void main(String[] args) {
PrivateDemo obj= new PrivateDemo ();
obj.first_name="James";
obj.last_name="Goosling";
obj.show();
}
}
Output:
First Name:=James
Last Name:=Goosling
private variable are not accessible out side the class :
package com.instanceofjava;
public class PrivateDemo {
private String first_name;
private String last_name;
void show(){
System.out.println("First Name:="+first_name);
System.out.println("Last Name:="+last_name);
}
public static void main(String[] args) {
PrivateDemo obj= new PrivateDemo ();
obj.first_name="James";
obj.last_name="Goosling";
}
}
package instanceofjava;
class Demo {
public static void main(String[] args){
PrivateDemo obj= new PrivateDemo ();
obj.add(); // ERROR: The method add() from the type PrivateDemo is not visible
}
}
private variables and methods are accessible inside that class only. If we declare any variable or method as private , not accessible outside the class.
2.protected
The class members which have protected keyword in its creation statements are called protected members. Those members can be accessible with in package from all classes, but from out side package only in subclass that too using subclass name or its object.
protected variables accessible inside the package anywhere. Outside package accessible only in sub classes.
Same package anywhere:
package com.instanceofjava;
public class ProtectedDemo {
protected int a;
protected int b;
protected void show(){
System.out.println("a="+a);
System.out.println("b="+b);
}
public static void main(String[] args) {
ProtectedDemo obj= new ProtectedDemo ();
obj.a=12;
obj.b=13;
obj.show();
}
}
Output:
a=12
b=13
Different package subclass:
package com.accesiblitymodifiers;
public class Sample extends ProtectedDemo {
public static void main(String[] args) {
Sample obj= new Sample();
obj.a=12;
obj.b=13;
obj.show();
}
}
Output:
a=12
b=13
3.public
If we declare any variable or method with public access specifier then those members will be accessible to everywhere.
package com.instanceofjava;
public class PublicDemo {
public int x;
public int y;
public void show(){
System.out.println("x="+x);
System.out.println("y="+y);
}
public static void main(String[] args) {
PublicDemo obj= new PublicDemo ();
obj.x=1;
obj.y=2;
obj.show();
}
}
Output:
x=1
y=2
4.default
If we declare any member with no keyword those members are called default members.
default members are accessible to package level.
Means we can access anywhere in same package but we can not access in out side the package under any condition.
So default will acts as public inside package and private out side the package.