Internal VS. Private Access Modifiers

Internal vs. Private Access Modifiers

internal is for assembly scope (i.e. only accessible from code in the same .exe or .dll)

private is for class scope (i.e. accessible only from code in the same class).

what is the difference between internal and private

An internal method can be accessed from any type (or function) in the same .NET assembly.

A private method can be accessed only from the type where it was declared.

Here is a simple snippet that shows the difference:

type A() = 
member internal x.Foo = 1

type B() =
member private x.Foo = 1

let a = A()
let b = B()
a.Foo // Works fine (we're in the same project)
b.Foo // Error FS0491: 'Foo' is not defined

What is the difference between private and protected Internal?

  • 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.

Swift 3: The difference between Public and Internal access modifiers?

Your diagram is just incorrect.

Public members of A.swift and B.swift are available to C.swift and D.swift. The only restriction is that classes can't be subclassed (they would need to be open.

In Swift, what is the difference between the Access modifiers internal and public?

Whatever you marked as public can be use within your app and outside of you app(module). If you marked something as internal that can only be used within your app(module). This is very helpful when your developing a library (framework) , you can use internal to hide library structure.

And Public members of A.swift and B.swift are available to C.swift and D.swift. The only restriction is that classes can't be subclassed (they would need to be open.)
- My answer base on @Keaz & @Alexander.

Public Vs Internal Access Modifier

Swift’s access control model is based on the concept of modules and source files.

A module is a single unit of code distribution—a framework or application that is built and shipped as a single unit and that can be imported by another module with Swift’s import keyword.

For example, some popular modules: Alamofire, SwiftyJson, RxSwift ...
These modules you are importing into your project. All classes that are using internal access level can be used only inside these libs/ modules. You cannot use them directly in your code.
But if the class is public - you can use it in your sources directly.

What is the difference between public, protected, package-private and private in Java?

The official tutorial may be of some use to you.





Leave a reply



Submit