diff --git a/Documentations/05 - Abstraction.md b/Documentations/05 - Abstraction.md index 6502498..d5d566e 100644 --- a/Documentations/05 - Abstraction.md +++ b/Documentations/05 - Abstraction.md @@ -4,48 +4,53 @@ Let's break this down with a simple example: -Consider a car. When you think about a car, you don't need to know every intricate detail of how the engine works or how the transmission shifts gears in order to drive it. Instead, you focus on the essential features like steering, accelerating, and braking. +Consider a car. When you think about a car, you don't need to know every detail of how the engine works or how the transmission shifts gears in order to drive it. Instead, you focus on the essential features like steering, accelerating, and braking. -In OOP, abstraction allows us to create a `Car` class that encapsulates these essential features without revealing the internal complexities. Here's a basic implementation: +In OOP, abstraction allows us to create a `Car` class that encapsulates these essential features from a vehicle without revealing the internal complexities. Here's a basic implementation: ```typescript -class Car { - private brand: string; - private model: string; - private speed: number; - - constructor(brand: string, model: string) { - this.brand = brand; - this.model = model; - this.speed = 0; - } +abstract class Vehicle { + protected speed: number = 0; + + // Abstract methods (no implementation) + abstract accelerate(): void; + abstract brake(): void; - public accelerate(): void { - this.speed += 10; + // Shared behavior + getSpeed(): number { + return this.speed; } +} - public brake(): void { - this.speed -= 10; +class Car extends Vehicle { + accelerate(): void { + this.speed += 20; // Cars accelerate faster } - public getSpeed(): number { - return this.speed; + brake(): void { + this.speed -= 20; } } -// Create a car object -const myCar: Car = new Car("Toyota", "Camry"); +class Bike extends Vehicle { + accelerate(): void { + this.speed += 10; // Bikes accelerate slower + } + + brake(): void { + this.speed -= 10; + } +} -// Accelerate the car -myCar.accelerate(); +// Usage +const vehicle: Vehicle = new Car(); -// Get the current speed -console.log("Current speed:", myCar.getSpeed()); +vehicle.accelerate(); +console.log("Speed:", vehicle.getSpeed()); ``` In this example: -We have a `Car` class with attributes like `make`, `model`, and `speed`. -We define methods like `accelerate` and `brake` to manipulate the speed of the car. -The user interacts with the car object through these methods without needing to know how they are implemented internally. +We create abstract class of `Vehicle` and create abstract methods only `accelerate` and `brake` without any implementation. +Then, we create a `Car` class and inherit Vehicle inside it. Now, Car will need to implement all the abstract methods defined in the abstract class of `Vehicle`. So, in essence, abstraction allows us to think about objects at a higher level of understanding, focusing on what they do rather than how they do it.