Sunday, December 28, 2014

Program to show the use of access specifiers

9:15 PM - By azeheruddin khan 0

Explanation: - An access specifier tells about the accessibility of a method, class or a variable. There are five different types of access specifiers: -


  1. Public: - any variable or method which is declared as public can be accessed anywhere within the namespace and also anywhere outside the namespace.
  2. Protected: - any variable or method which is declared as protected can be accessed by child classes within the namespace and also by child classes outside the namespace.
  3. Internal: - any variable or method which is declared as internal can be accessed anywhere within its namespace only.
  4. Protected internal: - any variable or method which is declared as Protected internal can be accessed anywhere within the namespace but only by child classes outside the namespace.
  5. Private: -any variable or method which is declared as private can be accessed only within its class.
Example:



using System;

namespace Example1
{
    class Program
    {
        static void Main(string[] args)
        {
            Access a = new Access();
            Console.WriteLine("Within Main() method of Program class of AccessSpecifiers namespace");
            a.publicmethod();
            a.internalmethod();
            a.protectedinternalmethod();
            Console.WriteLine();
            Console.WriteLine("Within view() method of child class:Access1 of Access class of AccessSpecifiers namespace");
            Access1 c = new Access1();
            c.view();
            Console.WriteLine();
            Console.ReadLine();

        }
    }

    public class Access
    {
        void privatemethod()
        {
            Console.WriteLine("private method() is accessible only within the class");
        }
        public void publicmethod()
        {
            Console.WriteLine("public method() is accessible everywhere within the namespace and outside the namespace");
        }
        internal void internalmethod()
        {
            Console.WriteLine("internal method() is accessible anywhere within the namespace");
        }
        protected internal void protectedinternalmethod()
        {
            Console.WriteLine("protectedinternal method() is accessible everywhere within the namespace but only by child classes of other namespace");
        }
        protected void protectedmethod()
        {
            Console.WriteLine("protected method() is accessible within the child classes of the namespace and also by child classes of other namespaces");
        }
    }
    class Access1 : Access
    {
        public void view()
        {
            protectedmethod();
            protectedinternalmethod();
            internalmethod();
        }
    }

}


Output:


Example:


using System;

namespace AccessExample2
{
    class Program
    {
        static void Main(string[] args)
        {
            Access e = new Access();
            e.publicmethod();
            Console.WriteLine();
            Program2 p = new Program2();
            Console.WriteLine("In AccessSpecifier1 namespace");
            Console.WriteLine("Within Program2 class, which is child class of Access class of AccessSpecifiers namespace");
            p.display();
            Console.ReadLine();

        }
    }

    public class Access
    {
        void privatemethod()
        {
            Console.WriteLine("private method() is accessible only within the class");
        }
        public void publicmethod()
        {
            Console.WriteLine("public method() is accessible everywhere within the namespace and outside the namespace");
        }
        internal void internalmethod()
        {
            Console.WriteLine("internal method() is accessible anywhere within the namespace");
        }
        protected internal void protectedinternalmethod()
        {
            Console.WriteLine("protectedinternal method() is accessible everywhere within the namespace but only by child classes of other namespace");
        }
        protected void protectedmethod()
        {
            Console.WriteLine("protected method() is accessible within the child classes of the namespace and also by child classes of other namespaces");
        }
    }

    class Program2 : Access
    {
        public void display()
        {
            protectedmethod();
            protectedinternalmethod();
            publicmethod();
        }
    }


}



Output:








About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

adsense

© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top