Friends Today MY Topic is Access Modifiers.This Topic Is Heart For Any Programming Language because every developer first know the usage of public,private,protected,internal,protected internal i simply say you know the access modifiers.The Access Modifiers are Common in All programming languages like c#.net,java,vb.net etc but small difference in usage.So,first we learn the C#.Net access Modifiers i place the best description for you.
Access Modifiers in C#.NET And Java
All Access Modifiers |
- Access Modifiers are public,private,protected,internal and protected internal.This access Modifiers are very important and precious topic because when we place public and where we place public and what has done place the public within assembly and what happening place place the public in outside the assembly.This types of queries are occur for every access modifiers .
- Access Modifiers topic very easy to understand and in this each modifier when its work like its work in inheritance,interface,abstract etc.
*************Introduction**************
Introduction
Access modifiers or Access specifiers are keywords which are specifying the accessability or access level of class members.
While defining a class or while declaring a class member we can define the accessibility with the help of access modifier.
In C#.Net we have 5 access modifiers. They are
1.Public
2.Private
3.Protected
4.Internal
5.Protected Internal
*************Public**************
1.Public
If we declare a class or class member as public which can be accessed by all the classes of current project and all the projects of application.
To access a public member there is no restriction with in the application
Ex:-class class1
{
public int a=10;
public void method1()
{
Console.WriteLine("method1 value is:"+a);
}
}
class class2
{
public void method2()
{
class1 obj=new class1();
Console.WriteLine("method2 value is:"+obj.a);
}
}
class Program
{
static void Main(String[] args)
{
class1 obj1=new class1();
Console.WriteLine("main method value is:"+obj1.a);
obj1.method();
class2 obj2=new class2();
obj2.method();
Console.ReadLine();
}
}
*************Private**************
2.Private
If we declare a class or class member as private, which can be accessed only with in that class.
Ex:-class class1
{
private int a=10;
public void method1()
{
Console.WriteLine("method1 value is:"+a);
}
}
class Program
{
static void Main(String[] args)
{
class1 obj1=new class1();
obj1.method1();
Console.ReadLine();
}
}
*************Protected**************
3.Protected
If we declare a class or class member as protected which can be accessed by current class members as well as derived class members.
Protected is depending on inheritance concept.
Ex:-class class1
{
protected int a=10;
public void method1()
{
Console.WriteLine("method1 value is:"+a);
}
}
class class2:class1
{
public void method2()
{
Console.WriteLine("method1 value is:"+a)
}
}
class Program
{
static void Main()
{
class2 obj=new class2();
class2 obj=new class2();
obj.method2();
obj.method1();
Console.ReadLine();
}
}
}
*************Internal**************
4.Internal
If we declare a class or class member as internal which can be accessed by all the classes of current project.
Ex:-class class1
{
internal int a=10;
public void method1()
{
Console.WriteLine("method1 value is:"+a);
}
}
class class2
{
public void method2()
{
class1 obj=new class1();
Console.WriteLine("method2 value is:"+obj.a);
}
}
class Program
{
static void Main(String[] args)
{
class1 obj1=new class1();
Console.WriteLine("main method value is:"+obj1.a);
obj1.method1();
class2 obj2=new class2();
obj2.method2();
Console.ReadLine();
}
}
Share With Your Friends in Social Media (Facebook, Twitter, Google Plus Etc...) if You Like Our Content And Feel Free To Comment.
0 comments:
Post a Comment