Skip to content

Commit 0c9531f

Browse files
committed
Refactor docs, removed redundancy
1 parent 9213396 commit 0c9531f

File tree

3 files changed

+0
-233
lines changed

3 files changed

+0
-233
lines changed

7-OOP/2-Abstraction/Readme.md

-124
Original file line numberDiff line numberDiff line change
@@ -5,127 +5,3 @@ Abstraction is one of the key principles in Object-Oriented Programming (OOP). I
55
## Abstraction in Python
66

77
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 import ABC, abstractmethod
17-
18-
class Shape(ABC):
19-
"""Abstract base class representing a shape."""
20-
21-
@abstractmethod
22-
def area(self) -> float:
23-
"""Calculate the area of the shape."""
24-
pass
25-
26-
class Square(Shape):
27-
"""Class representing a square."""
28-
29-
def __init__(self, side_length: float) -> None:
30-
self.side_length = side_length
31-
32-
def area(self) -> float:
33-
"""Calculate the area of the square."""
34-
return self.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 import ABC, abstractproperty
44-
45-
class Vehicle(ABC):
46-
"""Abstract base class representing a vehicle."""
47-
48-
@abstractproperty
49-
def max_speed(self) -> float:
50-
"""Maximum speed of the vehicle."""
51-
pass
52-
53-
class Car(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-
def max_speed(self) -> float:
61-
return self._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 import ABC, abstractmethod
71-
72-
class Animal(ABC):
73-
"""Abstract base class representing an animal."""
74-
75-
@abstractmethod
76-
def sound(self) -> str:
77-
"""Produce the sound of the animal."""
78-
pass
79-
80-
def move(self) -> str:
81-
"""Describe how the animal moves."""
82-
return "The animal moves."
83-
84-
class Dog(Animal):
85-
"""Class representing a dog."""
86-
87-
def sound(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 import ABC, abstractmethod
99-
100-
class PaymentGateway(ABC):
101-
"""Interface for payment gateways."""
102-
103-
@abstractmethod
104-
def process_payment(self, amount: float) -> bool:
105-
"""Process a payment."""
106-
pass
107-
108-
class PayPal(PaymentGateway):
109-
"""Class representing the PayPal payment gateway."""
110-
111-
def process_payment(self, amount: float) -> bool:
112-
"""Process a payment using PayPal."""
113-
print(f"Processing payment of ${amount} via PayPal.")
114-
return True
115-
116-
class Stripe(PaymentGateway):
117-
"""Class representing the Stripe payment gateway."""
118-
119-
def process_payment(self, amount: float) -> bool:
120-
"""Process a payment using Stripe."""
121-
print(f"Processing payment of ${amount} via Stripe.")
122-
return True
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.

7-OOP/3B-Encapsulation/Readme.md

-38
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,3 @@ Encapsulation is another fundamental principle in Object-Oriented Programming (O
55
## Encapsulation in Python
66

77
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
12-
13-
```python
14-
class Person:
15-
"""Class representing a person."""
16-
17-
def __init__(self, name: str, age: int) -> None:
18-
self.__name = name
19-
self.__age = age
20-
21-
def get_name(self) -> str:
22-
"""Get the name of the person."""
23-
return self.__name
24-
25-
def set_name(self, name: str) -> None:
26-
"""Set the name of the person."""
27-
self.__name = name
28-
29-
def get_age(self) -> int:
30-
"""Get the age of the person."""
31-
return self.__age
32-
33-
def set_age(self, age: int) -> None:
34-
"""Set the age of the person."""
35-
self.__age = age
36-
37-
person = Person("Alice", 30)
38-
print("Name:", person.get_name())
39-
print("Age:", person.get_age())
40-
41-
person.set_name("Bob")
42-
person.set_age(25)
43-
print("Updated Name:", person.get_name())
44-
print("Updated Age:", person.get_age())
45-
```

7-OOP/5-Polymorphism/Readme.md

-71
Original file line numberDiff line numberDiff line change
@@ -5,74 +5,3 @@ Polymorphism is a key concept in Object-Oriented Programming (OOP) that allows o
55
## Polymorphism in Python
66

77
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-
class Animal:
17-
"""Class representing an animal."""
18-
19-
def sound(self) -> str:
20-
"""Produce the sound of the animal."""
21-
return "Some generic animal sound."
22-
23-
class Dog(Animal):
24-
"""Class representing a dog."""
25-
26-
def sound(self) -> str:
27-
"""Produce the sound of the dog."""
28-
return "Woof!"
29-
30-
class Cat(Animal):
31-
"""Class representing a cat."""
32-
33-
def sound(self) -> str:
34-
"""Produce the sound of the cat."""
35-
return "Meow!"
36-
37-
def make_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-
class Bird:
53-
"""Class representing a bird."""
54-
55-
def fly(self) -> str:
56-
"""Fly like a bird."""
57-
return "Flying high."
58-
59-
class Airplane:
60-
"""Class representing an airplane."""
61-
62-
def fly(self) -> str:
63-
"""Fly like an airplane."""
64-
return "Soaring through the sky."
65-
66-
def lift_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

Comments
 (0)