From 05268addbe448319ce4a906ecb311ba358aab41f Mon Sep 17 00:00:00 2001 From: Abdullah Khan Kakar <2022bbit71@student.uet.edu.pk> Date: Sat, 28 Feb 2026 06:58:40 +0500 Subject: [PATCH 1/2] Update 05 - Abstraction.md --- Documentations/05 - Abstraction.md | 51 ++++++++++++++++-------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/Documentations/05 - Abstraction.md b/Documentations/05 - Abstraction.md index 6502498..c0ba1d8 100644 --- a/Documentations/05 - Abstraction.md +++ b/Documentations/05 - Abstraction.md @@ -9,43 +9,48 @@ Consider a car. When you think about a car, you don't need to know every intrica 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: ```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 { + private 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. 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. From 5b0301d117c8db00bce844e654ebd58c96c14035 Mon Sep 17 00:00:00 2001 From: Abdullah Khan Kakar <2022bbit71@student.uet.edu.pk> Date: Sat, 28 Feb 2026 08:12:15 +0500 Subject: [PATCH 2/2] Update 05 - Abstraction.md --- Documentations/05 - Abstraction.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Documentations/05 - Abstraction.md b/Documentations/05 - Abstraction.md index c0ba1d8..d5d566e 100644 --- a/Documentations/05 - Abstraction.md +++ b/Documentations/05 - Abstraction.md @@ -4,13 +4,13 @@ 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 abstract class Vehicle { - private speed: number = 0; + protected speed: number = 0; // Abstract methods (no implementation) abstract accelerate(): void; @@ -51,6 +51,6 @@ console.log("Speed:", vehicle.getSpeed()); In this example: -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.