Monday, February 21, 2022

What are different access controls in swift language?

Ans : 


To access the members inside an entity in a controlled manner,
we use the Access Controls
There are totally 5 different access specifiers.

1. open 
2. public
3. internal (default)
4. fileprivate
5. private

1. open -
Enables to access members of a module outside. 
Enable to subclass and override the classes in outside module as well.

2. public -
Allows us to subclass or override from within module in which it defined. 
But outside module, just we can utilize the functionality as it is without overriding.

Note:
So open class and class members can be accessible and overridden in which it is defined
and also in which module it is imported. public class and class members can be accessible
and overridden only in which it is defined.

3. internal -
Internal classes and members can be accessed anywhere within the same 
module(target) they are defined. 
You typically use internal access when defining an app’s or a framework’s internal structure.

4. fileprivate - 
Restricts the use of an entity to its defining file. It is used to hide
implementation details when details are used in entire file. fileprivate method is only 
accessible from that swift file in which it is defined.

5. private -
Restricts the use of an entity to the enclosing declaration and to extension
 of that swift file or class. It is used to hide single block implementation. private entity
 can be accessible in swift 4 but it gives error in swift 3.


No comments:

Post a Comment