Explain about Protected and protected internal, ?internal?
access-specifier?
Answer Posted / ranganathkini
Protected access specifier implies that the class member is
accessible to other members of the same class or to members
of a class that derieves from the class the member
originates from. example:
namespace Ranga.AccessTest {
class ClassA {
protected void MethodA() {
}
}
class ClassB : ClassA {
public void MethodB() {
base.MethodA(); // invoke MethodA()
}
}
class ClassC {
public void MethodC() {
// ClassC is not subclass of ClassA
// Hence it cannot access MethodA
ClassA myA = new ClassA();
myA.MethodA(); // ERROR
}
}
}
Protected internal access-specifier implies that the class
member is accessible in its originating class as well as
other class in the same assembly (i.e. EXE or DLL ). This
means that the member's access is protected OR internal.
Example.
// The foll. classes will be compiled into Program1.dll
namespace Ranga.Program1 {
public class ClassA {
protected internal void MethodA() {
}
}
public class ClassB : ClassA {
public void MethodB() {
base.MethodA(); // invoke MethodA()
}
}
public class ClassC {
public void MethodC() {
// Since ClassC is now in the same assembly
// as ClassA, MethodA is now accessible to it
// as it is marked as protected OR internal
ClassA myA = new ClassA();
myA.MethodA(); // NO ERROR HERE
}
}
}
// The foll. classes will be compiled into Program2.exe
// add reference to Program1.dll while compiling
using Ranga.Program1;
namespace Ranga.Program2 {
class ClassD {
public void SomeMethod1() {
// ERROR because ClassD is not in the same
// assembly as ClassA, MethodA is not accessible
// to it.
ClassA myA = new ClassA();
myA.MethodA(); // ERROR
}
}
// Now observe this
class ClassE : ClassA {
public void SomeMethod2() {
// We can call MethodA of ClassA here
// though ClassE is in a different assembly
// it is a derieved class of ClassA, hence
// MethodA() becomes accessible to it.
base.MethodA() // NO ERROR HERE
}
}
}
Hope it helps!!!
| Is This Answer Correct ? | 52 Yes | 4 No |
Post New Answer View All Answers
Write a console application and implement the ternary operator to decide whether the age a user entered after being prompted is allowed to vote or not(given that only citizens between 18 and 120 years only inclusive can vote). Use exception handling for non-numerical input.
What standard types does c# use?
What is scope c#?
How long does it take to learn c# programming?
What is an assembly qualified name? Is it a filename? How is it different?
what is generics? can u explain 5 to 6 examples on generics that covers class,method,structure,list,delegates?
What are the types of comments in c#?
What is singleordefault?
Is c and c# the same?
What is exe in c#?
How can you use abstract class and interface?
What is the real use of interface in c#?
What is interface inheritance in c#?
Explain how to implement an object pool in c#.net
What is icollection in c#?