TheBulgarianWolf

Visibility Modifiers

Apr 4th, 2021
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.71 KB | None | 0 0
  1. open class Base() {
  2.     var a = 1                 // public by default
  3.     private var b = 2         // private to Base class
  4.     protected open val c = 3  // visible to the Base and the Derived class
  5.     internal val d = 4        // visible inside the same module
  6.  
  7.     protected fun e() { }     // visible to the Base and the Derived class
  8. }
  9.  
  10. class Derived: Base() {
  11.  
  12.     // a, c, d, and e() of the Base class are visible
  13.     // b is not visible
  14.  
  15.     override val c = 9        // c is protected
  16. }
  17.  
  18. fun main(args: Array<String>) {
  19.     val base = Base()
  20.  
  21.     // base.a and base.d are visible
  22.     // base.b, base.c and base.e() are not visible
  23.  
  24.     val derived = Derived()
  25.     // derived.c is not visible
  26. }
Add Comment
Please, Sign In to add comment