Walkthrough - Kotlin: Visibility Modifiers
In this lesson, we explored Kotlin's four visibility modifiers: public, internal, protected, and private. We compared them to their Java counterparts and learned how Kotlin uses them to enforce encapsulation at the file, class, and module levels.
Changes Made
Implementation
- Created VisibilityModifiers.kt which implements:
- Top-level Visibility: Demonstrating
private(file scope) andinternal(module scope). - Class Member Visibility:
private: Accessible only within the class.protected: Accessible within the class and its subclasses (NOT the same package).internal: Accessible within the same module.public: Accessible everywhere (default).
- Constructor Visibility: Using the
private constructorsyntax with a factory method. - Local Declarations: Confirming that local variables cannot have visibility modifiers.
- Top-level Visibility: Demonstrating
- Updated App.java to execute the visibility demonstration.
Verification
- Build: Successfully executed
./gradlew :app:assemble. - Logic: Verified that
protectedmembers are not visible to unrelated classes even within the same package, confirming Kotlin's stricter encapsulation compared to Java.
See the Step-by-Step Explanation for technical details.
Comments
Post a Comment