Saturday 20 February 2016

what is Access modifier in java with example

Access is members with respective to other class, data and member. It is the way of specifying the accessibility.




Types of Access Modifier:


There are four basic Access modifier in java
  1. public
  2. private
  3. protected and
  4. default

    1. public : It is define if top level of interface within a package is declared as public. Then it means it is accessible inside and outside of the package. It is accessible anywhere class is visible by public.




    For Example:-

    Create a java class which name of class is A_Class.java



    package com;



    /**

     *

     * @author pradeep

     */

    public class A_Class {

      

    public void msssageShow()

    {

        System.out.println("Hello I am A_Class");

    }  






    Create a java class which name of class is B_Class.java





    package puter;

    import com.*;

    /**

     *

     * @author pradeep

     */

        

    class B_Class{  

      public static void main(String args[])

      {  

       A_Class obj = new  A_Class();  

       obj.msssageShow();  

      }  

    }   



    Result : Output of this program is


    run:

    Hello I am A_Class

    BUILD SUCCESSFUL (total time: 1 second)



    2. Private : This class is visible only one like as admin. They are accessible if and only if its is same class.


    For Example

    Create a java class which name of class is A_private_Class.java


    /**

     *

     * @author pradeep

     */



    class A_private_Class

    {  

    private int data=10;  

    private void massage()

    {

        System.out.println("Hello I am A_private_Class ");

    }  

    }  





    Create a java class which name of class is B_Show_Class.java



    /**
     *
     * @author pradeep
     */

    public class B_Show_Class{  
     public static void main(String args[]){  
       A_private_Class obj=new A_private_Class();  
       System.out.println(obj.data);
    //Show Compile Time Error  
       obj.massage();
    //Show Compile Time Error  
       }  

    ResultCompile time error show on your windows.


    3. protected : This class is visible package and outside of package. If members are declared  as protected then these are accessible to all the classes in the package and its to all subclasses package is visible.


    Example

    Create a java class which name of class is A_Protected_Class.java
     package com;

    /**
     *
     * @author pradeep
     */
    public class A_Protected_Class 
    protected void massage()
    {
        System.out.println("Hello I am A_Protected_Class");
    }  
    }  

    ~~~~~~~~~~~~~~~~******^^^^^^^^******~~~~~~~~~~~~~~~~

    **
    Create a java class which name of class is B_Protected_Class.java

    package puter;
    import com.*;
    /**
     *
     * @author pradeep
     */

    class B_Protected_Class extends A_Protected_Class
    {  
      public static void main(String args[])
      {  
       B_Protected_Class obj = new B_Protected_Class();  
       obj.massage();  
      }  
    }  

      
    Result : Output of this program is

    run:
    Hello I am A_Protected_Class
    BUILD SUCCESSFUL (total time: 4 seconds) 


    4. default : It is accessible only to the other classes in the class's or package.

    For Example:


    **
    Create a java class which name of class is A_Default_Class.java


    package com;



    /**

     *

     * @author pradeep

     */


    class A_Default_Class{  

      void massage()

      {

          System.out.println("Hello I am A_Default_Class");

      }  

    }  



    **

    Create a java class which name of class is B_Default_Class.java


    package puter;
    import com.*; 
    /**
     *
     * @author pradeep
     */
    class B_Default_Class
    {  
      public static void main(String args[])
      {  
       A_Default_Class obj = new A_Default_Class();
    //Show Compile Time Error  
       obj.massage();
    //Show Compile Time Error  
      }  
    }  




    Result : Output of this program is


    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - com.A_Default_Class is not public in com; cannot be accessed from outside package

    at puter.B_Default_Class.main(B_Default_Class.java:17)

    Java Result: 1

    BUILD SUCCESSFUL (total time: 2 seconds)



    No comments:

    Post a Comment