Kotlin program | Example of Various Visibility Modifiers on Package Level

Kotlin | Example of Various Visibility Modifiers: Here, we are implementing a Kotlin program to demonstrate the example of Example of various visibility modifiers on package level.
Submitted by IncludeHelp, on June 13, 2020

Various Visibility Modifiers on Package Level

  • Visibility modifiers are that set the accessibility (visibility) of Classes, objects, interfaces, constructors, functions, properties, and their setters.
  • There are four types of visibility modifiers,
    1. Public
    2. Private
    3. Protected
    4. Internal
  • Getters always have the same visibility as the properties.
  • Visibility Modifiers inside Packages:
    Functions, properties, and classes, objects and interfaces can be declared on the "top-level", i.e. directly inside a package,
    1. public: Visible everywhere, The default visibility, if there is not an explicit modifier
    2. private: Visible inside the file containing the declaration.
    3. internal: Visible everywhere with the same module.
    4. protected: Not available for top-level declaration.
  • Local variables, functions, and classes can not have visibility modifiers.
  • To specify the visibility of the primary constructor of a class, use the following syntax (by default constructors are public),
    class C private constructor(a: Int) { ... }
    

Program to demonstrate the example of Example of various visibility modifiers on package level in Kotlin

package com.includehelp

// Public by default, visible to everywhere
fun fun1(){
    // local variable can not have visibility
    var a=5
    println("Inside Fun1 $a")
}

// private , visible inside Containing file
// VisibilityModifiersInsidePackages.kt
private fun fun2(){
    println("Inside Fun2")
}

// internal , visible inside same module
internal fun fun3(){
    println("Inside Fun3")
}

// public by default visible everywhere
var name="IncludeHelp"

// by default public, visible every where
class MyClass

// Main function, Entry point of program
fun main(){
    // call function
    fun1()

    // call function
    fun2()

    // call function
    fun3()

    //Access variable
    println("Name : $name")
}

Output:

Inside Fun1 5
Inside Fun2
Inside Fun3
Name : IncludeHelp





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.