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: 7-OOP/2-Abstraction/Readme.md
-124
Original file line number
Diff line number
Diff line change
@@ -5,127 +5,3 @@ Abstraction is one of the key principles in Object-Oriented Programming (OOP). I
5
5
## Abstraction in Python
6
6
7
7
In Python, abstraction can be achieved through the use of classes and methods. By defining classes with methods that represent the behaviors of objects, we can abstract away the implementation details from the users of those objects.
8
-
9
-
### Code Samples
10
-
11
-
Here are four different code samples in Python to demonstrate abstraction:
12
-
13
-
#### 1. Abstract Base Class (ABC) with Abstract Method
14
-
15
-
```python
16
-
from abc importABC, abstractmethod
17
-
18
-
classShape(ABC):
19
-
"""Abstract base class representing a shape."""
20
-
21
-
@abstractmethod
22
-
defarea(self) -> float:
23
-
"""Calculate the area of the shape."""
24
-
pass
25
-
26
-
classSquare(Shape):
27
-
"""Class representing a square."""
28
-
29
-
def__init__(self, side_length: float) -> None:
30
-
self.side_length = side_length
31
-
32
-
defarea(self) -> float:
33
-
"""Calculate the area of the square."""
34
-
returnself.side_length **2
35
-
36
-
square = Square(5)
37
-
print("Area of the square:", square.area())
38
-
```
39
-
40
-
#### 2. Abstract Property
41
-
42
-
```python
43
-
from abc importABC, abstractproperty
44
-
45
-
classVehicle(ABC):
46
-
"""Abstract base class representing a vehicle."""
47
-
48
-
@abstractproperty
49
-
defmax_speed(self) -> float:
50
-
"""Maximum speed of the vehicle."""
51
-
pass
52
-
53
-
classCar(Vehicle):
54
-
"""Class representing a car."""
55
-
56
-
def__init__(self, max_speed: float) -> None:
57
-
self._max_speed = max_speed
58
-
59
-
@property
60
-
defmax_speed(self) -> float:
61
-
returnself._max_speed
62
-
63
-
my_car = Car(200)
64
-
print("Max speed of the car:", my_car.max_speed)
65
-
```
66
-
67
-
#### 3. Abstract Class with Concrete Methods
68
-
69
-
```python
70
-
from abc importABC, abstractmethod
71
-
72
-
classAnimal(ABC):
73
-
"""Abstract base class representing an animal."""
74
-
75
-
@abstractmethod
76
-
defsound(self) -> str:
77
-
"""Produce the sound of the animal."""
78
-
pass
79
-
80
-
defmove(self) -> str:
81
-
"""Describe how the animal moves."""
82
-
return"The animal moves."
83
-
84
-
classDog(Animal):
85
-
"""Class representing a dog."""
86
-
87
-
defsound(self) -> str:
88
-
return"Woof!"
89
-
90
-
dog = Dog()
91
-
print(dog.sound())
92
-
print(dog.move())
93
-
```
94
-
95
-
#### 4. Interface Abstraction
96
-
97
-
```python
98
-
from abc importABC, abstractmethod
99
-
100
-
classPaymentGateway(ABC):
101
-
"""Interface for payment gateways."""
102
-
103
-
@abstractmethod
104
-
defprocess_payment(self, amount: float) -> bool:
105
-
"""Process a payment."""
106
-
pass
107
-
108
-
classPayPal(PaymentGateway):
109
-
"""Class representing the PayPal payment gateway."""
110
-
111
-
defprocess_payment(self, amount: float) -> bool:
112
-
"""Process a payment using PayPal."""
113
-
print(f"Processing payment of ${amount} via PayPal.")
114
-
returnTrue
115
-
116
-
classStripe(PaymentGateway):
117
-
"""Class representing the Stripe payment gateway."""
118
-
119
-
defprocess_payment(self, amount: float) -> bool:
120
-
"""Process a payment using Stripe."""
121
-
print(f"Processing payment of ${amount} via Stripe.")
122
-
returnTrue
123
-
124
-
paypal_gateway = PayPal()
125
-
stripe_gateway = Stripe()
126
-
127
-
print(paypal_gateway.process_payment(100))
128
-
print(stripe_gateway.process_payment(150))
129
-
```
130
-
131
-
These examples demonstrate how abstraction allows us to define interfaces and hide implementation details, providing a clear separation between what an object does and how it does it.
Copy file name to clipboardExpand all lines: 7-OOP/3B-Encapsulation/Readme.md
-38
Original file line number
Diff line number
Diff line change
@@ -5,41 +5,3 @@ Encapsulation is another fundamental principle in Object-Oriented Programming (O
5
5
## Encapsulation in Python
6
6
7
7
In Python, encapsulation is typically implemented using classes. By defining attributes as private (prefixed with double underscores `__`) and providing public methods to access or modify these attributes, encapsulation can be achieved. This ensures that the internal state of an object remains hidden from the outside world and can only be accessed or modified through well-defined interfaces.
8
-
9
-
### Code Example
10
-
11
-
#### Private Attributes with Getter and Setter Methods
Copy file name to clipboardExpand all lines: 7-OOP/5-Polymorphism/Readme.md
-71
Original file line number
Diff line number
Diff line change
@@ -5,74 +5,3 @@ Polymorphism is a key concept in Object-Oriented Programming (OOP) that allows o
5
5
## Polymorphism in Python
6
6
7
7
In Python, polymorphism is achieved through method overriding and method overloading. Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. Method overloading, however, is not directly supported in Python as it is in languages like Java or C++, but Python offers a flexible way to achieve similar functionality through default arguments or variable-length arguments.
8
-
9
-
### Code Samples
10
-
11
-
Here are two code samples in Python to demonstrate polymorphism:
12
-
13
-
#### 1. Method Overriding
14
-
15
-
```python
16
-
classAnimal:
17
-
"""Class representing an animal."""
18
-
19
-
defsound(self) -> str:
20
-
"""Produce the sound of the animal."""
21
-
return"Some generic animal sound."
22
-
23
-
classDog(Animal):
24
-
"""Class representing a dog."""
25
-
26
-
defsound(self) -> str:
27
-
"""Produce the sound of the dog."""
28
-
return"Woof!"
29
-
30
-
classCat(Animal):
31
-
"""Class representing a cat."""
32
-
33
-
defsound(self) -> str:
34
-
"""Produce the sound of the cat."""
35
-
return"Meow!"
36
-
37
-
defmake_sound(animal: Animal) -> None:
38
-
"""Make the given animal sound."""
39
-
print(animal.sound())
40
-
41
-
# Polymorphic behavior based on object types
42
-
dog = Dog()
43
-
cat = Cat()
44
-
45
-
make_sound(dog) # Output: Woof!
46
-
make_sound(cat) # Output: Meow!
47
-
```
48
-
49
-
#### 2. Duck Typing
50
-
51
-
```python
52
-
classBird:
53
-
"""Class representing a bird."""
54
-
55
-
deffly(self) -> str:
56
-
"""Fly like a bird."""
57
-
return"Flying high."
58
-
59
-
classAirplane:
60
-
"""Class representing an airplane."""
61
-
62
-
deffly(self) -> str:
63
-
"""Fly like an airplane."""
64
-
return"Soaring through the sky."
65
-
66
-
deflift_off(entity):
67
-
"""Make the given entity fly."""
68
-
return entity.fly()
69
-
70
-
# Polymorphic behavior based on methods
71
-
bird = Bird()
72
-
airplane = Airplane()
73
-
74
-
print(lift_off(bird)) # Output: Flying high.
75
-
print(lift_off(airplane)) # Output: Soaring through the sky.
76
-
```
77
-
78
-
These examples demonstrate how polymorphism allows different objects to be treated uniformly through a common interface, enabling code reuse, flexibility, and extensibility in software design.
0 commit comments