CMPE-103-Module-6-Abstraction-and-Encapsulation
CMPE-103-Module-6-Abstraction-and-Encapsulation
Module 6
ABSTRACTION AND
ENCAPSULATION
Campus
class Employee: Encapsulation allows us to restrict accessing
# constructor variables and methods directly and prevent
def __init__(self, name, salary, project): accidental data modification by creating private
# data members data members and methods within a class.
self.name = name
self.salary = salary
self.project = project
Encapsulation is a way can restrict access to
methods and variables from outside of class.
# method Whenever we are working with the class and
# to display employee's details dealing with sensitive data, providing access to all
def show(self): variables used within the class is not a good choice.
# accessing public data member
print("Name: ", self.name, 'Salary:', self.salary)
# method
def work(self):
print(self.name, 'is working on', self.project)
print('Name:', emp.name)
# direct access to private member using name mangling
print('Salary:', emp._Employee__salary)
Protected Member
Protected members are accessible within the class and also available to its sub-classes. To define a protected
member, prefix the member's name with a single underscore _. Protected data members are used when you
implement inheritance and want to allow data members access to only child classes.
# base class
class Company:
def __init__(self):
# Protected member
self._project = "NLP"
# child class
class Employee(Company):
def __init__(self, name):
self.name = name
Company.__init__(self)
def show(self):
print("Employee name :", self.name)
# Accessing protected member in child class
print("Working on project :", self._project)
c = Employee("Jessa")
c.show()
# getter method
def get_age(self):
return self.__age
# setter method
def set_age(self, age):
self.__age = age
# getter methods
def get_stud_no(self):
return self.__stud_no
# before Modify
jessa.show()
# changing student number using setter
jessa.set_stud_no(120)
jessa.set_stud_no(25)
jessa.show()
Advantages of Encapsulation
•Security: The main advantage of using encapsulation is the security of the data.
Encapsulation protects an object from unauthorized access. It allows private and
protected access levels to prevent accidental data modification.
•Data Hiding: The user would not be knowing what is going on behind the scene. They
would only be knowing that to modify a data member, call the setter method. To read a
data member, call the getter method. What these setter and getter methods are doing
is hidden from them.
•Aesthetics: Bundling data and methods within a class makes code more readable and
maintainable
Try and run this programming problem
(The Fan class). Design a class named Fan to represent a fan. The class contains:
■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote
the fan speed.
■ A private int data field named speed that specifies the speed of the fan.
■ A private bool data field named on that specifies whether the fan is on (the default is
False).
■ A private float data field named radius that specifies the radius of the fan.
■ A private string data field named color that specifies the color of the fan.
■ The accessor(getters) and mutator(setters) methods for all four data fields.
■ A constructor that creates a fan with the specified speed (default SLOW), radius (default
5), color (default blue), and on (default False).
Write a test program named TestFan that creates two Fan objects. For the first object, assign
the maximum speed, radius 10, color yellow, and turn it on. Assign medium speed, radius 5,
color blue, and turn it off for the second object. Display each object’s speed, radius, color,
Programming Exercise:
A. Car Class
Write a class named Car that has the following data attributes:
• _ _year_model (for the car’s year model)
• _ _make (for the make of the car)
• _ _speed (for the car’s current speed)
The Car class should have an _ _init_ _ method that accepts the car’s year model and make as arguments. These values
should be assigned to the object’s _ _year_model and _ _make data attributes. It should also assign 0 to the _ _speed data
attribute.
Next, design a program that creates a Car object then calls the accelerate method five times. After each call to the
accelerate method, get the current speed of the car and display it. Then call the brake method five times. After each call to
the brake method, get the current speed of the car and display it.
B. Pet Class
Write a class named Pet, which should have the following data attributes:
• _ _name (for the name of a pet)
• _ _animal_type (for the type of animal that a pet is. Example values are ‘Dog’, ‘Cat’, and ‘Bird’)
• _ _age (for the pet’s age)
The Pet class should have an _ _init_ _ method that creates these attributes. It should also have the following methods:
• set_name()
This method assigns a value to the _ _name field.
• set_animal_type()
This method assigns a value to the _ _animal_type field.
• set_age()
This method assigns a value to the _ _age field.
• get_name()
This method returns the value of the _ _ name field.
• get_animal_type()
This method returns the value of the _ _animal_type field.
• get_age()
This method returns the value of the _ _age field.
Once you have written the class, write a program that creates an object of the class and prompts the user to enter the name,
type, and age of his or her pet. This data should be stored as the object’s attributes. Use the object’s accessor methods to
retrieve the pet’s name, type, and age and display this data on the screen.