Modifiers

4 0 0
                                        

visibility modifiers - it tells how much access you can have on a class for example inside a package. You can put these modifiers in interfaces, classes, objects, constructors, functions and properties.

Hierarchy

1. Module - it contains kotlin files.

2. file - it contains packages

3. package - it contains interface, objects, class, function and properties.

4. class - it contains primary constructor, secondary constructor, init, properties, functions, nested class, companion objects, objects, interface.

5. function - anything declared within a function body will be treated as local. Some of these are local variable, parameters, local functions and local class.

Top level declaration are those inside the package but independent on itself. These are class, object, interface, functions and properties.

Modifiers in top level declaration

1. public - this is the default. It can be viewed and accessed by all.

2. internal - visible only in the same module.

3. private - visible only inside the file it contains. So, only those in that file can accessed it.


Note: 

1. In the getters and setters, which is the get() and set() method of a property, the get() method has always the same modifiers as its property.

2. To use a top level declaration from one package to another is through importing.


Modifiers within a class

1. public - any program who uses the class has an access to it.

2.  internal - any program within the module can have access to it. 

3. protected - almost private to the class except it adds accessibility to its subclass.

4. private - accessible within the class only.


Note: 

1. Outer class has no access to the private members of its inner class. The inner class mentioned is the nested class within a class.

2. This is for protected and internal rule only which is during overriding, the compiler will assume that you want the same modifier as with the original if you did not specify it explicitly.

3. For constructors, the default modifier is public.

4. Local variables, functions and classes are not allowed to have modifiers. These mentioned locals are those declared within a function.

Kotlin ProgrammingWhere stories live. Discover now