56.1.1. OOPS Concepts - Data Hiding
(a) The Object Oriented Programming evolved to overcome the limitations of procedural programming approach.
(b) Data abstraction refers to the act of representing essential features without including the background details and explanations.
(c) Data encapsulation is the way of combining both data and functions that operate on the data under a single unit.
(d) The insulation of data from direct access by the program is called data hiding.
56.1.2. data hiding -public and private access modifiers
(a) When we write an object oriented python program, we can restrict the access to methods and variables.
(b) A private variable can only be accessed by a method that is of the same class and not outside of the class.
(d) A public variable/method can be accessed from anywhere.
56.1.3. Writing an example that uses the private variables
#define a class Student
class Student:
#define two private variables __group, __report with default values
__group = "ECE"
__report = "fail"
#define the __init__ method
def __init__(self,name,age):
self.name = name
self.age = age
#define the setDetails method with the two private variables
def setDetails(self,__group,__report):
self.__group = __group
self.__report = __report
#define the getDetails method
def getDetails(self):
print(self.name,self.age,self.__group,self.__report)
#take the inputs from the user
name = input("Name: ")
age = input("age: ")
group = input("group: ")
report = input("pass/fail: ")
print("Student Report Card")
#create an instance s1 of Student with name and age as arguments
s1 = Student(name,age)
#call the setDetails method with group and report as arguments
s1.setDetails(group,report)
#call the getdetails method with s1 object
s1.getDetails()
56.1.4. Writing public variables and methods.
#define a class Student
class Student:
#define the method setDetails that sets name, age, group, reports
def setDetails(self,name,age,group,report):
self.name = name
self.age = age
self.group = group
self.report = report
#define showDetails method that prints the details
def showDetails(self):
print(self.name,self.age,self.group,self.report)
name = input("name: ")
age = input("age: ")
group = input("group: ")
report = input("report: ")
#create an instance of the Student class
s = Student()
#call the setDetails method
s.setDetails(name,age,group,report)
#call the showDetails method
s.showDetails()
56.1.5. Write a program with private variables and public methods
class Car:
#define the private variables
__engineno = "EK1"
__modelno = "VX6"
#define the public method setcarinfo
def setcarinfo(self, engineno, modelno):
self.__engineno = engineno
self.__modelno = modelno
#define the public method getcarinfo
def getcarinfo(self):
print(self.__engineno, self.__modelno, self.color, self.year)
#create an instance hondacity of Car
hondacity = Car()
#take input engine number from the user
engnumber = input("engine number: ")
#call the setcarinfo method with the input, "SX6" as arguments
hondacity.setcarinfo(engnumber, "SX6")
#set the color to "Blue"
hondacity.color = "Blue"
#set the year to "2018"
hondacity.year = "2018"
#call the getcarinfo method
hondacity.getcarinfo()