Skip to content

Commit 28ea84e

Browse files
Update Abstraction.md
1 parent eb58b14 commit 28ea84e

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

week_11/Abstraction.md

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,48 @@
11
### Abstraction
22

3-
Abstraction is the concept of hiding complex implementation details and showing only the necessary features of an object. It simplifies interactions by focusing on what an object does rather than how it does it.
3+
Abstraction is the concept of hiding complex implementation details and showing only the necessary system features. We use abstract classes to define what must be done, not how.
44

5-
For example, when you use a smartphone, you don’t need to understand the internal circuits and software. You simply use the phone's interface to make calls, send messages, or browse the internet. The intricate details are hidden, allowing you to focus on the phone’s features.
5+
For example, you don’t need to understand the internal circuits and software when you use a smartphone. You use the phone's interface to make calls, send messages, or browse the internet. The internal details are hidden, allowing you to focus on the phone’s features.
66

77
### Two Ways of Abstraction
88

9-
1. **Abstract Class**:
10-
- An abstract class is a class that cannot be instantiated on its own and is meant to be subclassed. It can include abstract methods (methods without implementation) that must be implemented by subclasses.
11-
- **Python Example**:
12-
13-
```python
14-
from abc import ABC, abstractmethod
15-
16-
class Vehicle(ABC):
17-
@abstractmethod
18-
def start(self):
19-
pass
20-
21-
@abstractmethod
22-
def stop(self):
23-
pass
24-
25-
class Car(Vehicle):
26-
def start(self):
27-
print("Car is starting")
28-
29-
def stop(self):
30-
print("Car is stopping")
31-
32-
my_car = Car()
33-
my_car.start() # Output: Car is starting
34-
my_car.stop() # Output: Car is stopping
9+
1. **Abstract Class**
10+
- An abstract class is a class you **cannot use directly** to create objects.
11+
- It is made to be used by other classes (called child classes).
12+
- It can have **abstract methods** — these are like empty functions with no code inside.
13+
- The child class must **write the real code** for those methods.
14+
15+
**🧠 Python Example:**
16+
17+
```python
18+
from abc import ABC, abstractmethod
19+
20+
# Abstract class
21+
class Vehicle(ABC):
22+
@abstractmethod
23+
def start(self):
24+
pass
25+
26+
@abstractmethod
27+
def stop(self):
28+
pass
29+
30+
# Child class
31+
class Car(Vehicle):
32+
def start(self):
33+
print("Car is starting")
34+
35+
def stop(self):
36+
print("Car is stopping")
37+
38+
my_car = Car()
39+
my_car.start() # Output: Car is starting
40+
my_car.stop() # Output: Car is stopping
41+
opping
3542
```
3643

3744
2. **Abstract Method**:
38-
- An abstract method is a method defined in an abstract class without an implementation. Subclasses that inherit from the abstract class must provide their own implementation of these methods.
45+
- An abstract method is a method that is defined in an abstract class without implementation details. Subclasses that inherit from the abstract class provide the implementation of abstract method.
3946
- **Python Example**:
4047

4148
```python
@@ -58,6 +65,3 @@ For example, when you use a smartphone, you don’t need to understand the inter
5865
print(my_rectangle.area()) # Output: 20
5966
```
6067

61-
In these examples:
62-
- The **abstract class** `Vehicle` defines methods `start` and `stop` without implementing them. The subclass `Car` must provide implementations for these methods.
63-
- The **abstract method** `area` in the `Shape` class is implemented by the `Rectangle` subclass, which provides the specific logic for calculating the area of a rectangle.

0 commit comments

Comments
 (0)