‫Welcome back.
‫In this video we'll learn about visibility modifiers.
‫In Kotlin, visibility modifiers are used to control the visibility and access levels of classes, properties,
‫functions, and other elements within your code.
‫Kotlin provides for visibility, modifiers, public, private, protected, and internal.
‫Let's start with the Public Visibility modifier.
‫This is the default visibility modifier, and it means that the element is visible everywhere, both
‫inside and outside the current model.
‫For this example and for this exercise, I'll start with a new variable called serialnumber.
‫It's of type string equals to 12345.
‫If you don't specify any visibility modifier by default, Kotlin will recognize this serial number variable
‫as public.
‫Also, you can use the public modifier.
‫This element is visible everywhere.
‫If you go to the hello Kotlin KTX file and I need to access firing Robert de dot serial number, I can
‫access it and I can modify it okay.
‫Let's move to the second visibility modifier, which is the private elements marked as private are only
‫visible within the file or class where they are declared.
‫They are not accessible from other classes or files.
‫Let me mark this serial number as private.
‫Let me go back to hello Kotlin and you can see there is an error.
‫Can't access serial number.
‫It's invisible private in the supertype in firing robot.
‫So I can't access the serial number from another class or file.
‫When using the private keyword.
‫Protected elements marked as protected are visible within the current class and its subclasses.
‫The inheritance, but not outside.
‫Let me show you.
‫Here I have three classes that extends from General Robot.
‫So these are the subclasses of the general robot.
‫If I tried in the Firing robot class, let me show you do access here in the fire method super dot.
‫Let me modify it as protected and then go back to the fire serial number equals to 8888.
‫I can access the serial number in the subclasses because it's marked as protected with a visibility
‫modifier.
‫But if we go to hello Kotlin file and trying to access this serial number, we don't have anything named
‫as serial number because it's marked as protected, only visible within the class and its subclasses.
‫The third visibility modifier is the internal elements marked as internal are visible within the same
‫module.
‫A module is a set of Kotlin files that are compiled together.
‫Let me mark this serial number as internal.
‫And here if we go to hello Kotlin and try to use firing robot dot serial number, I can access it.
‫Because if we go to the project structure, we can see that hello, Kotlin act file is placed in the
‫same package with the other four classes.
‫So hello, Kotlin Act file can access the fields that are marked with internal in these classes.
‫And as mentioned before I can mark fields.
‫I can mark methods with the visibility modifiers.
‫I can mark classes with these modifiers.
‫So visibility modifiers are very important and allow you to control the visibility and access level
‫of classes, properties, functions and other elements within your code.