Skip to main content

3.3 - Encapsulation and Abstraction

3.3.1 - Introduction to Encapsulation

Encapsulation in Python involves bundling data (variables) and methods (functions) that operate on the data into a single unit, a class. It also restricts direct access to some of the object's components, which is a way of preventing accidental modification of data.

Example 1: Basic Encapsulation

class Computer:
def __init__(self):
self.__max_price = 900 # Private attribute

def sell(self):
return f"Selling Price: ${self.__max_price}"

def set_max_price(self, price):
if price > 800:
self.__max_price = price

c = Computer()
print(c.sell()) # Selling Price: $900
c.set_max_price(1000)
print(c.sell()) # Selling Price: $1000

Example 2: Encapsulation with Private Methods

class Account:
def __init__(self, balance):
self.__balance = balance # Private variable

def __update_balance(self, amount): # Private method
self.__balance += amount

def deposit(self, amount):
if amount > 0:
self.__update_balance(amount)
return "Deposit successful"

account = Account(100)
print(account.deposit(50)) # Deposit successful

3.3.2 - Introduction to Abstraction

Abstraction in Python means hiding the complex reality while exposing only the necessary parts. It is achieved by using abstract classes and methods that serve as a blueprint for other classes.

Example 1: Basic Abstraction

from abc import ABC, abstractmethod

class Vehicle(ABC):
@abstractmethod
def move(self):
pass

class Car(Vehicle):
def move(self):
return "Car is moving"

class Boat(Vehicle):
def move(self):
return "Boat is sailing"

car = Car()
boat = Boat()
print(car.move()) # Car is moving
print(boat.move()) # Boat is sailing

Example 2: Abstraction with Multiple Abstract Methods

class ElectronicDevice(ABC):
@abstractmethod
def turn_on(self):
pass

@abstractmethod
def turn_off(self):
pass

class Smartphone(ElectronicDevice):
def turn_on(self):
return "Smartphone turning on"

def turn_off(self):
return "Smartphone turning off"

phone = Smartphone()
print(phone.turn_on()) # Smartphone turning on
print(phone.turn_off()) # Smartphone turning off

3.3.3 - Combining Encapsulation and Abstraction

Using encapsulation and abstraction together can create a robust and maintainable code structure.

Example 1: Using Private Attributes in Abstract Class

class ElectronicDevice(ABC):
def __init__(self):
self.__status = "off" # Private attribute

def get_status(self):
return self.__status

@abstractmethod
def turn_on(self):
pass

class Television(ElectronicDevice):
def turn_on(self):
return "Television turning on"

tv = Television()
print(tv.get_status()) # off
print(tv.turn_on()) # Television turning on

Example 2: Abstract Methods with Encapsulation Logic

class Payment(ABC):
def final_amount(self, amount):
return amount - self.discount(amount)

@abstractmethod
def discount(self, amount):
pass

class CardPayment(Payment):
def discount(self, amount):
return amount * 0.05 # 5% discount

payment = CardPayment()
print(payment.final_amount(100)) # 95