Visibility modifiers (public, private, internal, protected)
📖 Scenario: You are creating a simple Kotlin class to understand how different visibility modifiers control access to class members.
🎯 Goal: Build a Kotlin class with properties and functions using public, private, internal, and protected visibility modifiers and observe how they affect access.
📋 What You'll Learn
Create a Kotlin class named
Car with properties and functions using all four visibility modifiers.Add a variable to hold the car's brand with
public visibility.Add a variable to hold the car's engine number with
private visibility.Add a function with
internal visibility that returns the car's brand.Add a
protected function that returns the engine number.Create a subclass
SportsCar that tries to access the protected function.Print outputs to show which members are accessible.
💡 Why This Matters
🌍 Real World
Visibility modifiers help protect sensitive data and control how parts of your code can be used or changed by others.
💼 Career
Understanding visibility is essential for writing safe, maintainable Kotlin code in professional Android or backend development.
Progress0 / 4 steps