Explain about Protected and protected internal, ?internal?
access-specifier?
Answers were Sorted based on User's Feedback
Answer / 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 |
Answer / diana cheriyan
Protected:-Access is limited to the Containing class or
types derived from the Containing class
Internal:-Access is limited to the Current Assembly
Protected Internal:-Access is limited to the current
Assembly or types derived from the Containing class
Is This Answer Correct ? | 19 Yes | 7 No |
Answer / sumathi
Protectd :
Method declared as Protected,means it is accessible only in
it's derived classes.
So with this access specifier we can provide more security
to the class members.
Protected internal:
Methods or varibales are declared as protected internal are
accessible in the derived classes but outside the assembly
also.
Internal:
Member Methods or member variables declared as internal are
accessible with in classes of the same assembly.
Is This Answer Correct ? | 14 Yes | 4 No |
Answer / sudhir kumar
Protected access specifier allows a class to hide its member
variables and member functions from other class objects and
functions, except the child class.
The protected access specifier becomes important while
implementing inheritance.
Protected members are not Visible to objects of other class,
or other classes outside the namespace collection,and also
not visible to objects of child classes outside the
namespace collection.
Protected access specifier allows a class to hide its member
variables and member functions from other class objects and
functions, except the child class,with in the application.
The protected access specifier becomes important while
implementing inheritance.
Protected members are not Visible to objects of other class,
or other classes outside the namespace collection,and also
not visible to objects of child classes outside the
namespace collection.
Is This Answer Correct ? | 6 Yes | 4 No |
Mr.Ranganathkini,
Your explanation helped me a lot,
I need some more clarification.
suppose in your first example Class C is in namespace
Ranga.AccessTest , if it was not in the same namespace, but
let us assume that it has inherited the class A, then could
it have the access to methodA.
Is This Answer Correct ? | 3 Yes | 2 No |
What is an argument in c#?
What kind of the information stored inside the assembly?
Difference between value and reference type. What are value types and reference types?
What are Namespaces?
What is generic method in c#?
What is a predicate in c#?
What does void do in unity?
What is difference between array and collection?
What is manifest ?
What is a static in c#?
Contrast between an interface and abstract class?
Can you override private virtual methods?