Modifier's in Java

Modifier:

Two type of  Modifier's available in Java


  1. Access Modifiers
  2. Non-Access Modifiers

Non-Access Modifiers
 
      The non-access modifier are  
                                                          static
                                                          abstract
                                                          synchronized
                                                          native
                                                          volatile
                                                          transient
                                                          etc..,

Access Modifiers

  1. default
  2. private
  3. public
  4. protected
Default Modifier:

          If the programmer not mention the Modifier, then the compiler automatically take it as default access modifier. The default access modifier accessible with in the class and package.

Note: Default Modifier is not same as Public

Example:

package com.theworldofshine.default.first;     //first.java
class first
  {
      void run()
         {
            System.out.println("First program-default package");
          }
   }

package com.theworldofshine.default.second     // second.java
import com.theworldofshine.default.first;          //  importing package.
class second
  {
      public static void main(String args[])
         {
            first object=new first();
            object.run();
          }
  }

Output:

       Compiler error, because default is not accessible other package. 

Private:

        Private is accessible with-in class only.

Example:

package com.theworldofshine.private;        //    private_modifier_subclass.java

class private_modifier
  {
       int a=10;
   }

class private_modifier_subclass
   {
       public static void main(String args[])

          {
             private_modifier object=new private_modifier();
             System.out.println(object.a);
           }
     }

Output:

compiler error, because private is accessible with in class itself only.

Public:

     Public is accessible anywhere in the program, it can be accessible any package, class.

Example:

package com.theworldofshine.public.first;     //  First_public.java
class First_public
  {
      public void run()
         {
            System.out.println("First program-public example");
          }
   }

package com.theworldofshine.public.second     // Second_public.java
import com.theworldofshine.public.first;          //  importing package.
class Second_public
  {
      public static void main(String args[])
         {
            first object=new first();
            object.run();
          }
  }

Output:

First program-public example.

Protected:

     The protected modifier accessible any class, and outside of the package with inheritance. The protected is applied for data member, method and constructor but not for class*.

Example:

package com.theworldofshine.protected.first;     //  First_protected.java
class First_protected
  {
      protected void run()
         {
            System.out.println("First program-protected example");
          }
   }

package com.theworldofshine.protected.second     // Second_protected.java
import com.theworldofshine.protected.first;          //  importing package.
class Second_protected
  {
      public static void main(String args[])
         {
            first object=new first();
            object.run();
          }
  }

Output:

First program-protected example.

Difference between Access Modifiers


Access Modifier
within class
within package

outside package by subclass only
outside package
Private
Y
N
N
N
Default
Y
Y
N
N
Protected
Y
Y
Y
N
Public
Y
Y
Y
Y

No comments:

Post a Comment