Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "private" and "protected Internal"?

I just want to know what is the actual difference between private and protected internal access specifier. As i know

Visible to own class members: private and protected internal YES

Visible to object of other classes: Both NO

Visible to objects of other classes outside the namespace collection: Both NO

Visible to object of child classes outside the namespace collection: Both NO

If private doing the same as protected internal then why we need the both just one should be enough or not?

like image 309
avirk Avatar asked Jun 09 '12 10:06

avirk


People also ask

What is the difference between private protected and protected?

my understanding is that for protected - A protected member is accessible within its class and by derived class instances irrespective of the assembly, whereas for Private protected - A private protected member is accessible by types derived from the containing class, but only within its containing assembly.

What is the difference between private and internal?

Private: - Private members are only accessible within the own type (Own class). Internal: - Internal member are accessible only within the assembly by inheritance (its derived type) or by instance of class.

What is mean by protected internal?

A protected internal member of a base class is accessible from any type within its containing assembly. It is also accessible in a derived class located in another assembly only if the access occurs through a variable of the derived class type.

Is there any difference between public protection and private protection?

Broadly speaking, public means everyone is allowed to access, private means that only members of the same class are allowed to access, and protected means that members of subclasses are also allowed.


2 Answers

  • A protected internal member is visible to any code in the current assembly or in a derived class in another assembly. In technical words, it's the logical disjunction of protected and internal.
  • A private member is visible only to code in the same class.

protected internal is actually the second most permissive access modifier after public.


It's worth noting that protected is arguably more permissive than internal, since it allows access from code that you have no control over (i.e. other assemblies). While internal allows access from all code in the current assembly, this code is yours and you have control over it!

To paraphrase, protected (and protected internal) members are part of the public API of your assembly (and should therefore be documented). internal members are not.

like image 72
Will Vousden Avatar answered Oct 21 '22 21:10

Will Vousden


A graphical overview (summary in a nutshell)

Visibility

like image 20
Stefan Steiger Avatar answered Oct 21 '22 22:10

Stefan Steiger