Access modifiers public private protected
📖 Scenario: Imagine you are creating a simple Car class to manage car details. Some information should be open for everyone to see, some should be hidden inside the car, and some should be visible only to the car and its close family (subclasses).
🎯 Goal: You will build a Car class using public, private, and protected access modifiers. Then you will create a subclass ElectricCar to see how protected members work.
📋 What You'll Learn
Create a
Car class with public, private, and protected propertiesAdd a constructor to initialize the properties
Create a subclass
ElectricCar that extends CarAccess the protected property inside the subclass
Print the public property from an instance
Try to access private and protected properties outside the class to see errors (commented out)
💡 Why This Matters
🌍 Real World
Access modifiers help protect sensitive data in software, like hiding engine numbers or internal details of a car from outside code.
💼 Career
Understanding access modifiers is important for writing secure and maintainable code in many programming jobs, especially when working with object-oriented languages like TypeScript.
Progress0 / 4 steps