Object Oriented Programming In Python-
-
- Python is a fully object oriented programming language because it doesn’t support static data type concept where you need to declare the the data type at the time of variable creation.
- 1) Encapsulation
- 2) Inheritance
- 3) Abstraction(Python doesn’t support this but somehow we can do it)
- 4) Polymorphism
- 1) Encapsulation-
- – Protecting the object info from outside access.
- – To implement encapsulation, we have to follow below-
- 1) Class should be public
- 2) Variables should be private
- 3) We can access the info only via methods like setter() and getter()
Ex-
class Student:
def __init__(self, rollno, name):
self.__name = name
self.__rollno = rollno
def getName(self):
return self.__name
def getrollno(self):
return self.__rollno
def setrollno(self, rollno):
self.__rollno = rollno
print("Rollno was set")
def setName(self, name):
self.__name = name
print("Name was set")
def main():
obj = Student(101, 'Rohit')
obj.getName()
rollno = obj.getrollno()
print("RollNo before set-", rollno)
obj.setrollno(102)
rollno = obj.getrollno()
print("RollNo after set-", rollno)
print(id(obj.getrollno))
Student.main()
Output-
RollNo before set- 101
Rollno was set
RollNo after set- 102
2361410337992

