Home »
Kotlin »
Kotlin programs »
Kotlin class and object programs
Kotlin program | Example of Various Visibility Modifiers
Kotlin | Various Visibility Modifiers: Here, we are implementing a Kotlin program to demonstrate the example of various visibility modifiers.
Submitted by IncludeHelp, on June 12, 2020
Visibility Modifiers
Program to demonstrate the example of Various Visibility Modifiers in Kotlin
package com.includehelp
// Class, by default public visibility
// Mark with 'open' to make inheritable
open class MyOuter{
private var myname="IncludeHelp India !!"
protected open val age=30
internal var salary=100
var city="Delhi" // Public , by default
protected fun fun1(){
println("Protected Function in Base !!")
}
}
class MySub: MyOuter() {
// Override protected members,
// because protected is visible in subclass
override val age: Int
get() = 50
fun printDetails(){
// Can't Access $myname as it is private in super class
// println("Name : $myname")
println("City : $city")
println("Salary : $salary")
println("Age : $age")
}
}
// Main function, Entry Point of Program
fun main(){
// Create Instance
val myOuter = MySub()
// Call subclass method
myOuter.printDetails()
}
Output:
City : Delhi
Salary : 100
Age : 50