Protected Member:-
Protected Member of a class is only available
in the containing class (in which it has been declared) and in the derived
class within the same assembly and also outside the assembly.
Means if a class that resides outside the
assembly can use the protected member of the other assembly by inherited that
class only.
Note:
Protected members are not accessible using the object of class (in which
it is declared) in the derived class.
For example
namespace Entity
{
public class
Student
{
protected string Name { get; set;
}
}
public class
Detail:Student
{
public void
Display()
{
//using
base class object we can not access
Student obj = new Student();
//ite
give compile time errror
//Error
CS1540 Cannot access protected member
'Student.Name'
//via
a qualifier of type 'Student'; the qualifier
//must
be of type 'Detail' (or derived from it)
//obj.Name
= "";//error
//access
base class protected member using
//inheritance
only
Name = "Ravi";
}
}
}
We can exposed the Protected member outside
the assembly by inherited that class and use it in the derived class only.
//assembly
1
namespace Entity
{
public class
Student
{
protected string Name { get; set;
}
}
}
//assembly
2
namespace AnotherAssembly
{
//assembly
2 class which inherit "Entity.Student"
//and
then we can access protected member of student class
public class
Detail:Entity.Student
{
public void
Display()
{
//assembly
1 class's protected member
Name = "xyz";
}
}
}
Internal
Member:-
Internal Member of a class is available or
access within the assembly either creating object or in a derived class or you
can say it is accessible across all the
classes within the same assembly. for example
namespace Entity
{
public class
Student
{
internal string Name { get; set;
}
}
public class
Detail:Student
{
public void
Display()
{
//using
base class object we can access internal
member
Student obj = new Student();
obj.Name = "ravi";//not any error but in protected it give error
//access
base class internal member using
//inheritance
Name = "Ravi";
}
}
}
Note:
Internal members not accessible outside the assembly either using object
creating or in a derived class.
using Entity;
namespace Test
{
//another
assembly class
//try
to access internal member
public class
A:Student
{
void display()
{
Student obj = new Student();
//it
give error because we can not access internal member
//Error
CS0122 'Student.Name' is inaccessible
due to its protection level
//obj.Name
= "";//compile time error
//Error
CS0122 'Student.Name' is inaccessible
due to its protection level
//Name
= "";//compile time error
}
}
}
Protected
Internal:-
Protected Internal access modifier is
combination of Protected and Internal access modifier.
Protected Internal Member can be available
within the entire assembly in which it declared either creating object or by
inherited that class. And can be accessible outside the assembly in a derived
class only.
namespace Entity
{
public class
Student
{
protected internal string Name { get; set;
}
}
public class
Detail:Student
{
public void
Display()
{
//using
base class object we can access
Student obj = new Student();
obj.Name = "ravi";//not any error
//access
base class protected internal member using
//inheritance
Name = "Ravi";
}
}
}
In Another assembly
using Entity;
namespace Test
{
//another
assembly class
//try
to access protected internal member
public class
A:Student
{
void display()
{
A obj = new A();
obj.Name = "";//calling using child class object
Name = "";//without object we can call
}
}
}
Note:
Protected Internal member works as Internal within the same assembly and works
as Protected for outside the assembly.
No comments:
Post a Comment