본문 바로가기

Kotlin

#8.7 Kotlin Visibility Modifiers: public, protected, internal and private. Object Oriented Concepts

open class Person{
    private val a = 1
    protected val b = 2
    internal val c = 3
    val d = 10 // public by default
}

class Indian: Person(){
    // a is not visible
    // b, c, d is visible

    fun test(){
        print(b)
    }
}

 

class TestClass{
    
    // person.a, person.b are not visible
    // person.c, person.d are visible
    
    fun testing(){
        var person=Person()
        print(person.c)
    }
}

open class Person{
    private val a = 1
    protected val b = 2
    internal val c = 3
    val d = 10 // public by default
}

class Indian: Person(){
    // a is not visible
    // b, c, d is visible

    fun test(){
        print(b)
    }
}

기본값으로 public 이다 어디에서든 값이 보인다.

private 는 그 안에서만 값이 보인다. 

protected 는 하위 class에서만 값이 보인다.

internal 은 같은 모듈에서는 값이 보인다. 

youtu.be/0JT2M2jb7Lo