Protected modifier is not for top level declarations.Protected members will be only accessible by the declaring class and sub classes. Any class inside the module who sees the declaring class can access its internal members. They will only be visible inside the same module. Internal modifier is a module level modifier. If we declare a class as private, it will only be visible to the classes declared inside the same file. That means, if we declare a method or variable private, it will only be visible inside the class it belongs to. The idea is to hide the internal implementation logic as much as possible. Private modifier limits the visibility to the file or class containing the declaration. Any class who sees the declaring class can access its public members. Public members are visible to all other classes. If we do not type any modifier, visibility will be public. The default visibility modifier is public. We have public, private, protected and internal. Similarly in Kolin we have visibility modifiers. The internal modifier make the field accessible only inside the module in which it is implemented.> Github Link to download the code project Kotlin Visibility Modifiers The internal modifier are newly added in Kotlin, it is not available in Java. Output The value of integer is: 30 Internal Println("The overridden value of integer is: "+a.getvalue()) Open protected val int = 20 // protected variable In the below example, we will see how to overridden the int variable. Some time we need to add open keyword to the the protected variable or function to override in the derived class. class CompanionClass output The value of integer is: 10 Overriding of protected modifier To create a companion object in kotlin, you just need to add the companion keyword in front of the object declaration.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |