Home > DeveloperSection > Blogs > Access specifier and modifier in java

Access specifier and modifier in java


Java Java 
Ratings:
0 Comment(s)
 113  View(s)
Rate this:

Access specifier and modifier in java

There are 4 types of access specifier in java:

  • Private
  • public
  • protected
  • default

Private:

Program which contains private specifier for specific function or class or datatype, it can be accessible only for that class or function. It is not accessible by other class or assemblies.

 

Public:


Program which contains public specifier for function or class or datatype, it can be accessible anywhere within the program or in other assemblies also.

 

Protected:


Program in which in protected specifier is specified for function or class or datatype, it can be accessible only for that class or function. It is not accessible by other class or assemblies. But it can be accessible in the derived class.

 

Default:


It is similar to INTERNAL access specifier in C#. Program in which in no specifier is specified for function or class or datatype then default specifier is  it can be used anywhere within the program. But it is not accessible by other assemblies.

 

Public

 

class access

    {

       public String Stra;

       public void print()

        {

            System.out.println("MindStick is better than" + Stri);

        }

    }

 

    class Program

    {

        static void Main(String[] args)

        {

            access sp = new access();

            System.out.println ("Enter the name of the company :");

            sp.Stri = System.console().readLine();

            sp.print();

        }

    }

}

 

Private

 

class access

   {

        private String Strp;

        public void print()

        {

           System.out.println ("MindStick is better than " + Stri);

        }

    }

 

    class Program

    {

        static void Main(String[] args)

        {

            access sp = new access();

            System.out.println ("Enter the name of the company:");

            sp.Stri = System.console().readLine();// error will occur

            sp.print(); 

            

        }

    }

}

 

Program for Protected Specifier

 

class access
    {      

        Protected String Stri;

        public void print()

        {

           System.out.println ("MindStick is better than " + Stri);

        }

    }

 

    class Program

    {

        static void Main(String[] args)

        {

            access sp = new access();

            System.out.println ("Enter the name of the company:");        

            sp.Stri = System.console().readLine();// error will occur

            sp.print(); 

            Console.ReadLine();

        }

    }



Program for Default Specifier:

 

 class access 

    { 

       String Stri;

        public void print()

        {

           System.out.println ("MindStick is better than " + Stri);

        }

    }

 

    class Program

    {

        static void Main(String[] args)

        {

            access sp = new access();

           System.out.println ("Enter the name of the company:"); 

            sp.Stri = System.console().readLine();

            sp.print();

            Console.ReadLine();

        }

    }

 

Whereas in C# there are 5 access specifier:

 

Private

Public

Protected

Internal

Protected Internal


For more details please click here : basic concept for access specifier

 


Don't want to miss updates? Please click the below button!

Follow MindStick