Encapsulation and Polymorphism
Encapsulation restricts direct access to some of an object's components, often using private variables.
class Account:
def __init__(self):
self.__balance = 0 # private variable
def deposit(self, amount):
self.__balance += amount
def get_balance(self):
return self.__balance
Polymorphism allows different classes to be treated through a common interface.
class Cat:
def sound(self):
print("Meow")
class Dog:
def sound(self):
print("Bark")
def make_sound(animal):
animal.sound()
make_sound(Cat()) # Meow
make_sound(Dog()) # Bark