Access
specifiers are used to restrict the data access. Kotlin support 4 access
specifiers.
a.
private
b.
protected
c.
internal
d.
public
If
you do not specify any access specifier, then public is used by default.
Where can I apply
access specifiers?
You
can apply access specifiers on Classes, interfaces, constructors, objects, functions, properties and their setter methods. Getters always have the same
visibility as the property.
Below
table summarizes the access specifiers.
Access Specifier
|
Description
|
Private
|
Members
defined with private access specifier are visible only in same scope.
|
Protected
|
Members
defined with protected access specifier are visible in the same class and all
the sub classes of this class.
|
Internal
|
Members
defined with internal access specifier are visible in the same module.
|
Public
|
Members
defined with public access specifier are visible from everywhere.
|
public access
specifier
If
you mark an element with public modifier, then this element can be accessed from
anywhere. If you do not specify any access modifier to an element, the access
is public by default.
Test.kt
package com.sample.model class Test { public var i : Int = 10 var j : Int = 11 }
HelloWorld.kt
import com.sample.model.Test fun main(args: Array<String>) { var obj = Test() println("i = ${obj.i}") println("j = ${obj.j}") obj.i = 88 obj.j = 99 println("i = ${obj.i}") println("j = ${obj.j}") }
Output
i = 10 j = 11 i = 88 j = 99
private access
specifier
If
you declare an element using private access specifier, then the code that is
declared in the same scope can access it, no other code can access it.
Test.kt
package com.sample.model private var totalObjs: Int = 0 class DemoClass1 { constructor() { totalObjs++ } companion object { fun getTotalObjs() = totalObjs } } class DemoClass2 { constructor() { totalObjs++ } }
HelloWorld.kt
import com.sample.model.* fun main(args: Array<String>) { var obj1 = DemoClass1() var obj2 = DemoClass2() println("Total objects created : ${DemoClass1.getTotalObjs()}") var obj3 = DemoClass1() var obj4 = DemoClass2() println("Total objects created : ${DemoClass1.getTotalObjs()}") }
Output
Total objects created : 2 Total objects created : 4
Protected access
specifier
Members
defined with protected access specifier are visible in the same class and all
the sub classes of this class.
Test.kt
open class DemoClass1 { protected var i: Int = 10 } class DemoClass2 : DemoClass1() { fun getValueOfI(): Int { return i } }
Note
If
you override a protected member and do not specify the visibility explicitly,
the overriding member will also have protected visibility.
internal access
specifier
Members
defined with internal access specifier are visible in the same module. A module
is a collection of kotlin resources compiled together.
Test.kt
open class DemoClass1 { internal var i: Int = 10 } class DemoClass2 : DemoClass1() { fun getValueOfI(): Int { return i } }
Note
a. Local variables,
local functions and classes do not have access specifiers
No comments:
Post a Comment