Check Mobile App
Read now
Start a Project

Visibility Modifiers in Kotlin

Save

Visibility modifiers in Kotlin are keywords that set the visibility of classes, objects, interfaces, constructors, functions, and properties.

Types of Visibility Modifiers

There are Four types of visibility modifiers in Kotlin.

The purpose of modifiers is to provide accessibility to classes, objects, functions, etc.
Basically, there is no need to set modifiers for getters as they have the same visibility as their properties.
The public is the default modifier.
Also,If you don’t define any modifier it will serve as a Default modifier.

Public Modifier

It is the default visibility modifier in Kotlin.
Classes, variables, and interfaces are visible everywhere which are public.
It is also a frequently used modifier.
Generally used when no need to implement restrictions on field function and variables.

For Example

 Private Modifier

Private modifiers are only allowed within the block in which property is declared.
It does not allow outside access.
A private package can only be accessible within that specific file.
Generally used when implementing more restrictions on field function and variables.
For Example

 Protected Modifier

Protected modifier with class and interface only visible to its class or subclass
An overridden, protected declaration in its subclass will also be a protected modifier until we change explicitly.
Generally used when implementing more restrictions on field function and variables.

 Internal Modifier

The internal modifier makes the field visible only inside the module.
Error Will through if we access any field outside the module.
Generally used with Api-related Work.

For Example

Conculusion

In this blog, we have discussed about the Visibility modifiers in Kotlin

Thank you for reading!!

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

css.php

Great Product, Great Team, and Great Support Service. And if you want to add more features to the product, they can submit any idea that comes to your mind. They really care about their clients and we are really happy and honored to deal with Webkul.

Osama
Talk to Sales

Global

Live Chat
Start a Project

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home