Ads Sponsored By Google :)

Sunday, 27 January 2013

public|protected|default|private access specifiers in java with examples|table


access specifiers in java access specifiers in java with examples access specifiers in java table protected access specifier in java with example default access specifier in javawith example public access specifier in java with example private access specifier in java with example protected access specifier in java
public access specifier in java default access specifier in java private access specifier in java


     Access Specifiers:

           Access Specifiers are those which are applied before data members methods of a 
      class / interface. In java programming we have four access specifiers. They are
1)      Private
2)      Default
3)      Protected
4)      Public
Access specifiers always makes us to understand how to access the features (data members and methods) with in package and across package. Or between class to class or interface to interface or interface to class.

Syntax for declaring and initializing a variable a long with access specifier:

  [Access-Specifier] [Static] [final] datatype v1[=value1],v2[=value2]…….vn[=valuen];

For example:

1)       Public static final float pi=3.14f;
2)      Privat static int pin=xxx;
3)      Protected static String sub=”java”;
4)      Static String institute=”JavaJavax”;

Syntax for declaring and initializing a variable a long with access specifier:

[Access-Specifier] [Static] [final] methodname(list of formal parmeter if required)
{
  Block of statements(s);
}

For Example:
  Public static void main(String args[])
{
-------------
}

Note:
1)      Private access specifier is known as native access specifier .
2)      Default access specifier is known as package access specifier.
3)      Protected access specifier is known as inherited access specifier.
4)      Public access specifier is also known as universal access specifier.

Access Specifiers Table:


      The Following program illustrate the concept of Access Specifier:

      Package javajavax;
     
      public  Class Base
     {
        Private int  x=90;
        int y=95;
        Protected  int z=99;
        Public int p=105;
        Public Base()
       {
        System.out.println(“value ofx is”+x);
        System.out.println(“value ofx is”+y);
        System.out.println(“value ofx is”+z);
        System.out.println(“value ofx is”+p);
       }
     }

     Package javajavax;

    Public class InterDerived extends Base
    {
      Public  InterDerived()
    {
     //System.out.println(“value of x”+x); //Not accessible
       System.out.println(“value ofx is”+y);// Default access specifier using in another class 
       System.out.println(“value ofx is”+z);// Protected access specifier using in another class 
      System.out.println(“value ofx is”+p);// public access specifier using in another class
     }
   }

    Compile the above packages as 
     C:\> javac -d . javajavax.java    
      then we get the .class of InterDerived and Base.

   // User defined packages used in Class Test As follows

   import javajavax.Base;
   import javajavax.InterDerived;   

    Class Test
  {
     Public static void main(String args[])
   {
     System.out.println(“w.r.t Base”);
     javajavax.Base s1=ew javajavax.Base();
     System.out.println(“w.r.t InterDerived”);
      javajavax.InterDerived();
      System.out.println(“w.r.t Derived”);
      javajavax.Derived();
     }
   }

No comments:

Post a Comment

LinkWithin