The Artima Developer Community
Sponsored Link

Java Buzz Forum
Accessibility modifiers examples

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Accessibility modifiers examples Posted: Mar 9, 2015 10:18 AM
Reply to this message Reply

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.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • 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 :


  1. package com.instanceofjava;
  2.  
  3. public class PrivateDemo {
  4.  
  5.     private String first_name;
  6.     private String last_name;
  7.  
  8. void show(){
  9.  
  10.   System.out.println("First Name:="+first_name);
  11.   System.out.println("Last Name:="+last_name);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        PrivateDemo obj= new PrivateDemo ();
  18.  
  19.         obj.first_name="James";
  20.         obj.last_name="Goosling";
  21.         obj.show();
  22.  
  23.     }
  24. }

Output:

  1. First Name:=James
  2. Last Name:=Goosling

private variable are not  accessible out side the class :

  1. package com.instanceofjava;
  2.  
  3. public class PrivateDemo {
  4.  
  5.     private String first_name;
  6.     private String last_name;
  7.  
  8. void show(){
  9.  
  10.   System.out.println("First Name:="+first_name);
  11.   System.out.println("Last Name:="+last_name);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        PrivateDemo obj= new PrivateDemo ();
  18.  
  19.         obj.first_name="James";
  20.         obj.last_name="Goosling";
  21.       
  22.  
  23.     }
  24. }

  1. package instanceofjava;
  2.  
  3. class Demo {
  4.  
  5. public static void main(String[] args){
  6.  
  7. PrivateDemo obj= new PrivateDemo ();
  8.  
  9.         obj.first_name="James"; // ERROR: The field PrivateDemo.first_name is not visible
  10.  }
  11.  
  12. }

private methods are accessible within the class :

  1. package com.instanceofjava;
  2.  
  3. public class PrivateDemo {
  4.  
  5.     private String first_name;
  6.     private String last_name;
  7.  
  8. private void show(){
  9.  
  10.   System.out.println("First Name:="+first_name);
  11.   System.out.println("Last Name:="+last_name);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        PrivateDemo obj= new PrivateDemo ();
  18.  
  19.         obj.first_name="James";
  20.         obj.last_name="Goosling";
  21.         obj.show();
  22.  
  23.     }
  24. }

Output:

  1. First Name:=James
  2. Last Name:=Goosling

private variable are not  accessible out side the class :

  1. package com.instanceofjava;
  2.  
  3. public class PrivateDemo {
  4.  
  5.     private String first_name;
  6.     private String last_name;
  7.  
  8. void show(){
  9.  
  10.   System.out.println("First Name:="+first_name);
  11.   System.out.println("Last Name:="+last_name);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        PrivateDemo obj= new PrivateDemo ();
  18.  
  19.         obj.first_name="James";
  20.         obj.last_name="Goosling";
  21.       
  22.  
  23.     }
  24. }

  1. package instanceofjava;
  2.  
  3. class Demo {
  4.  
  5. public static void main(String[] args){
  6.  
  7. PrivateDemo obj= new PrivateDemo ();
  8.  
  9.         obj.add(); // ERROR: The method add() from the type PrivateDemo is not visible
  10.  }
  11.  
  12. }

  • 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:

  1. package com.instanceofjava;
  2.  
  3. public class ProtectedDemo {
  4.  
  5.     protected int a;
  6.     protected int b;
  7.  
  8. protected void show(){
  9.  
  10.   System.out.println("a="+a);
  11.   System.out.println("b="+b);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        ProtectedDemo obj= new ProtectedDemo ();
  18.  
  19.         obj.a=12;
  20.         obj.b=13;
  21.         obj.show();
  22.  
  23.     }
  24. }

Output:

  1. a=12
  2. b=13

Different package subclass:

  1. package com.accesiblitymodifiers;
  2.  
  3. public class Sample extends ProtectedDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.        Sample  obj= new Sample();
  8.  
  9.         obj.a=12;
  10.         obj.b=13;
  11.         obj.show();
  12.  
  13.     }
  14. }

Output:

  1. a=12
  2. b=13

3.public

  • If we declare any variable or method with public access specifier then those members will be accessible to everywhere.


  1. package com.instanceofjava;
  2.  
  3. public class PublicDemo {
  4.  
  5.     public int x;
  6.     public int y;
  7.  
  8. public void show(){
  9.  
  10.   System.out.println("x="+x);
  11.   System.out.println("y="+y);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        PublicDemo obj= new PublicDemo ();
  18.  
  19.         obj.x=1;
  20.         obj.y=2;
  21.         obj.show();
  22.  
  23.     }
  24. }


Output:

  1. x=1
  2. 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.

Read: Accessibility modifiers examples

Topic: BoA shifting it's hardware strategy Previous Topic   Next Topic Topic: REST API Evolution

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use