You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: week_11/Abstraction.md
+36-32Lines changed: 36 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,41 +1,48 @@
1
1
### Abstraction
2
2
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.
4
4
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.
6
6
7
7
### Two Ways of Abstraction
8
8
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 importABC, abstractmethod
15
-
16
-
classVehicle(ABC):
17
-
@abstractmethod
18
-
defstart(self):
19
-
pass
20
-
21
-
@abstractmethod
22
-
defstop(self):
23
-
pass
24
-
25
-
classCar(Vehicle):
26
-
defstart(self):
27
-
print("Car is starting")
28
-
29
-
defstop(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 importABC, abstractmethod
19
+
20
+
# Abstract class
21
+
classVehicle(ABC):
22
+
@abstractmethod
23
+
defstart(self):
24
+
pass
25
+
26
+
@abstractmethod
27
+
defstop(self):
28
+
pass
29
+
30
+
# Child class
31
+
classCar(Vehicle):
32
+
defstart(self):
33
+
print("Car is starting")
34
+
35
+
defstop(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
35
42
```
36
43
37
44
2. **Abstract Method**:
38
-
- An abstract method is a method defined in an abstract class without an implementation. Subclasses that inherit from the abstract classmust provide their own implementation of these methods.
45
+
- An abstract method is a method that isdefined in an abstract class without implementation details. Subclasses that inherit from the abstract class provide the implementation of abstract method.
39
46
-**Python Example**:
40
47
41
48
```python
@@ -58,6 +65,3 @@ For example, when you use a smartphone, you don’t need to understand the inter
58
65
print(my_rectangle.area()) # Output: 20
59
66
```
60
67
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`classis implemented by the `Rectangle` subclass, which provides the specific logic for calculating the area of a rectangle.
0 commit comments