|
| 1 | +# The 4 Pillars of OOP in Python: |
| 2 | +# 3. Encapsulation |
| 3 | +# Encapsulation is wrapping the data (attributes) and the methods into one |
| 4 | +# single unit, known as the class. |
| 5 | +# This will control access to some data components and methods, prevent them |
| 6 | +# from direct access or modification (outside the class), and allow only the |
| 7 | +# methods inside to access and modify these data and methods. |
| 8 | +# This will lead us to the three types of data members. |
| 9 | +# There are three types of data (attributes and methods) can be defined inside |
| 10 | +# the class: |
| 11 | +# 1. Public Members, which are the attributes and methods that can be accessed |
| 12 | +# and modified directly, when creating an instance from the class. |
| 13 | + |
| 14 | +class Dogs: |
| 15 | + # Public attribute |
| 16 | + brand = "Husky" |
| 17 | + |
| 18 | + def __init__(self, name, age): |
| 19 | + # Public attribute |
| 20 | + self.name = name |
| 21 | + self.age = age |
| 22 | + |
| 23 | + # Public method |
| 24 | + def bark(self): |
| 25 | + print("Uf Uf") |
| 26 | + |
| 27 | + |
| 28 | +dog_obj = Dogs("Leo", 6) |
| 29 | +# print(dog_obj.name) |
| 30 | +# print(dog_obj.age) |
| 31 | +# print(dog_obj.brand) |
| 32 | +# print(dog_obj.bark()) |
| 33 | +dog_name = dog_obj.__dict__["name"] = "Silver" |
| 34 | +# print(dog_obj.name) |
| 35 | + |
| 36 | + |
| 37 | +# 2. Protected Members are only accessible inside the class itself, and also |
| 38 | +# can be accessed by the subclasses. |
| 39 | +# Protected Members' names should start with a single underscore "_" |
| 40 | +# Although the protected members can be accessed and modified out of the |
| 41 | +# class, programmers should refrain from accessing and modifying them |
| 42 | +# outside the class body. |
| 43 | + |
| 44 | +class DogsRev: |
| 45 | + |
| 46 | + def __init__(self): |
| 47 | + # Protected Attribute |
| 48 | + self._name = "Leo" |
| 49 | + self._age = 6 |
| 50 | + |
| 51 | + # Protected Method |
| 52 | + def _sound_age(self): |
| 53 | + return f"{self._name} is barking, his age is {self._age}" |
| 54 | + |
| 55 | + # Using the protected method inside another method |
| 56 | + def dog_property(self): |
| 57 | + return self._sound_age() |
| 58 | + |
| 59 | + |
| 60 | +class Dog(DogsRev): |
| 61 | + brand = "Husky" |
| 62 | + |
| 63 | + def dog_brand(self): |
| 64 | + return f"My dog {self._name} is a {Dog.brand}" |
| 65 | + |
| 66 | + |
| 67 | +dog_obj1 = Dog() |
| 68 | +# print(dog_obj1._name) |
| 69 | +# print(dog_obj1._sound_age()) |
| 70 | +# Can be accessed outside the class and used by subclasses, |
| 71 | +# but they shouldn't. |
| 72 | +# print(dog_obj1.dog_brand()) |
| 73 | +# print(dog_obj1.dog_property()) |
| 74 | + |
| 75 | + |
| 76 | +# 3. Private Members are members of the class, they can't be used, accessed, |
| 77 | +# or modified inside the subclasses, and can't be modified or accessed outside |
| 78 | +# the class, they can be only used inside the superclass or the class they are |
| 79 | +# defined in. |
| 80 | +# To define these private members use double underscores "__" before their |
| 81 | +# names. |
| 82 | + |
| 83 | +class DogsRev1: |
| 84 | + |
| 85 | + def __init__(self, name, age): |
| 86 | + self.name = name |
| 87 | + self.age = age |
| 88 | + # Private Attribute |
| 89 | + self.__bark = "barking" |
| 90 | + |
| 91 | + # Private Method |
| 92 | + def __sound_age(self): |
| 93 | + return f"{self.name} is {self.__bark}, his age is {self.age}" |
| 94 | + |
| 95 | + def dog_property(self): |
| 96 | + return self.__sound_age() |
| 97 | + |
| 98 | + |
| 99 | +class DogOne(DogsRev1): |
| 100 | + |
| 101 | + def sound(self): |
| 102 | + return super().__sound_age() |
| 103 | + |
| 104 | + def bark(self): |
| 105 | + return super().__bark |
| 106 | + |
| 107 | + |
| 108 | +dogs_parent = DogsRev1("Blade", 3) |
| 109 | +# print(dogs_parent.__bark) |
| 110 | +# print(dogs_parent.__sound_age()) |
| 111 | +# print(dogs_parent.dog_property()) |
| 112 | + |
| 113 | +dogs_child = DogOne("Blade", 3) |
| 114 | +# print(dogs_child.__bark) |
| 115 | +# print(dogs_child.sound()) |
| 116 | +# print(dogs_child.bark()) |
| 117 | +# print(dogs_child.dog_property()) |
| 118 | + |
| 119 | + |
| 120 | +# Thus Encapsulation emphasizes data hiding and controls how to access and |
| 121 | +# modify the class attributes and methods. |
0 commit comments