vertopal.com_3. Polymorphism and Encapsulation
vertopal.com_3. Polymorphism and Encapsulation
len("Ajay")
len([1, 2, 3, 4, 5])
len((1, 2))
'pwskills'
[1, 2, 3, 4, 5, 6]
class teacher_lecture:
def lec_info(self):
print("This is lec info with teacher perspective")
class student_lecture:
def lec_info(self):
print("This is lec info with student perspective")
obj1 = teacher_lecture()
obj2 = student_lecture()
class_obj = [obj1, obj2]
def parcer(class_obj):
for i in class_obj:
i.lec_info() #at this point, lec_info is taking two forms wrt
techer and students
parcer(class_obj)
This is lec info with teacher perspective
This is lec info with student perspective
#method overloading
class Student:
def student(self):
print("Wecome to pwskills class")
def student(self, name = ""):
print("Wecome to pwskills class", name)
def student(self, name = "", course = ""):
print("Wecome to pwskills class", name, course)
stud = Student()
stud.student()
stud.student("Ajay")
stud.student("Ajay", "DS")
stud = Student()
stud.student()
stud.student("Ajay")
stud.student("Ajay", "DS")
class Animal:
def sound(self):
print("Animal Sound")
class Cat(Animal):
def sound(self):
print("cat meows")
anm = Animal()
Animal Sound
cat = Cat()
cat.sound() #cat class
cat meows
class Student:
def __init__(self, name, degree):
self.name = name
self.degree = degree
stud1.name
'Ram'
stud1.degree
'Masters'
'Bach'
stud2.degree = "Phd"
stud2.degree
'Phd'
#accessing the public data member inside the other method of same
class
class Student:
def __init__(self, name, degree):
self.name = name
self.degree = degree
def show(self):
#accessing the public data member
print("name", self.name, 'degree', self.degree)
aj = Student("Ajay", "Masters")
aj.show()
aj.name
'Ajay'
aj.degree
'Masters'
#private >> the data and method is only accessible within its class,
use __ to make private
class Student:
def __init__(self, name, degree):
self.name = name
self.__degree = degree #private data
def show(self):
#accessing the private data member
print("name", self.name, 'degree', self.__degree)
aj = Student("Ajay", "Masters")
aj.name
'Ajay'
aj.show()
----------------------------------------------------------------------
-----
AttributeError Traceback (most recent call
last)
Cell In[67], line 1
----> 1 aj.degree #it will throw an error as degree is now private
data
----------------------------------------------------------------------
-----
AttributeError Traceback (most recent call
last)
Cell In[68], line 1
----> 1 aj.__degree #it will throw an error as degree is now private
data
'Masters'
#a method private
class Student:
def __init__(self, name, degree):
self.name = name
self.__degree = degree #private data
def show(self):
#accessing the private data member
print("name", self.name, 'degree', self.__degree)
def __private_method(self):
print("This is a private method")
----------------------------------------------------------------------
-----
AttributeError Traceback (most recent call
last)
Cell In[72], line 1
----> 1 obj1.private_method()
class Student:
def __init__(self, name, degree):
self.name = name
self.__degree = degree #private data
def show(self):
#accessing the private data member
print("name", self.name, 'degree', self.__degree)
def __private_method(self):
print("This is a private method")
def access_private_method(self):
self.__private_method()
----------------------------------------------------------------------
-----
AttributeError Traceback (most recent call
last)
Cell In[79], line 2
1 c1 = Car("1995", "Maruti", "80", "Brezza")
----> 2 c1.year
class Car:
def __init__(self, year, make, speed, model):
self.__year = year
self.__make = make
self.__speed = speed
self.__model = model
def set_speed(self, speed):
self.__speed = 0 if speed < 0 else speed
def get_speed(self):
return self.__speed
'80'
c1.set_speed(-1000)
c1.get_speed()
c1.set_speed(100)
c1.get_speed()
100
acc1 = Bank(1000)
acc1.get_balance()
1000
acc1.deposit(500)
acc1.get_balance()
1500
acc1.withdraw(100)
True
acc1.get_balance()
1400
acc1.withdraw(1500)
False
#protected>> within the class and its subclass, protected member can
be access, (_)
class College:
def __init__(self):
self._college_name = "PWSkills"
class Student(College):
def __init__(self, name):
self.name = name
College.__init__(self) #accesing variable of base
class>>classname__init__(self)
def show(self):
print("name", self.name, "college", self._college_name)
#directly call the base class variable using "_"
stud = Student("Ajay")
stud.name
'Ajay'
stud.show()
'PWSkills'