0
0
Typescriptprogramming~30 mins

Access modifiers public private protected in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
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 properties
Add a constructor to initialize the properties
Create a subclass ElectricCar that extends Car
Access 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
1
Create the Car class with properties
Create a class called Car with these properties: public brand: string, private engineNumber: string, and protected mileage: number. Add a constructor that takes brand, engineNumber, and mileage as parameters and sets the properties.
Typescript
Need a hint?

Use public, private, and protected keywords before the property names inside the class.

2
Create the ElectricCar subclass
Create a class called ElectricCar that extends Car. Add a constructor that takes brand, engineNumber, mileage, and batteryCapacity (number). Call the parent constructor with brand, engineNumber, and mileage. Add a public batteryCapacity: number property and set it.
Typescript
Need a hint?

Use extends Car to create the subclass and super(...) to call the parent constructor.

3
Access protected property inside subclass
Inside the ElectricCar class, add a method called showMileage that returns a string showing the mileage using the protected property mileage. Use return `Mileage is ${this.mileage} km`; inside the method.
Typescript
Need a hint?

Use this.mileage inside the method to access the protected property.

4
Create instance and print public property
Create an instance called myElectricCar of ElectricCar with brand 'Tesla', engineNumber 'E12345', mileage 15000, and batteryCapacity 85. Print the brand using console.log(myElectricCar.brand). Also print the mileage by calling console.log(myElectricCar.showMileage()). Comment out lines that try to access engineNumber or mileage directly from myElectricCar because they cause errors.
Typescript
Need a hint?

Use new ElectricCar(...) to create the instance and console.log to print the values. Comment out direct access to private and protected properties.