-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathVisibilityModifiers.kt
67 lines (54 loc) · 1.38 KB
/
VisibilityModifiers.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
public: Visible everywhere
private: Visible inside the same class only
internal: Visible inside the same module
protected: Visible inside the same class and its subclasses
*/
// By Default Public Class
class TeamOne {
var number = 100
}
// Specified with Public modifier
public class TeamTwo {
var number = 200
fun show() {
println("Accessible everywhere")
}
}
// TeamThree is accessible from same source file
private class TeamThree {
private val number = 50
fun show() {
// We can access number in the same class
println(number)
println("Accessing number successful")
}
}
// Internal modifier benefits in writing APIs and implementations.
internal class TeamFour {}
public class TeamFive {
internal val number = 300
internal fun show() {}
}
open class TeamSix {
// Protected variable
protected val number = 75
}
// Inherited Class
class TeamSeven : TeamSix() {
fun getValue(): Int {
// Accessed from the subclass
return number
}
}
fun main() {
val teamOne = TeamOne()
println("teamOne - Number : ${teamOne.number}")
val teamTwo = TeamTwo()
teamTwo.show()
val teamThree = TeamThree()
teamThree.show()
// println(teamThree.number) -> Can not access 'number': it is private in class TeamThree
val teamSeven = TeamSeven()
println("Value: " + teamSeven.getValue())
}