Unit 3 Lecture 1.2-Inheritance
Unit 3 Lecture 1.2-Inheritance
Lecture 1.2
Programming with
Python
S c h o o l o f C o m p u t i n g a n d I n f o r m a t i o n Te c h n o l o g y
Programming with Python
Recap of previous Lecture
RECAP OF PREVIOUS
LECTURE
Defining Classes
The_init_() Method
Instantiating Classes
Instance Variables
Programming with Python
Python Inheritance
PROGRAMMING WITH PYTHON
Python inheritance
Inheritance allows us to define a class that inherits all the methods and
properties from another class.
Parent class is the class being inherited from, also called base class.
Child class is the class that inherits from another class, also called derived
class.
PROGRAMMING WITH PYTHON
Python inheritance
Benefits of inheritance are:
Inheritance allows you to inherit the properties of a class, i.e., base class to
another, i.e., derived class. The benefits of Inheritance in python are as
follows:
1. It represents real-world relationships well.
2. It provides the reusability ouh11f a code. We don’t have to write the
same code again and again. Also, it allows us to add more features to a
class without modifying it.
3. It is transitive in nature, which means that if class B inherits from another
class A, then all the subclasses of B would automatically inherit from
class A.
4. Inheritance offers a simple, understandable model structure.
5. Less development and maintenance expenses result from an
inheritance.
PROGRAMMING WITH PYTHON
Python inheritance
Class BaseClass:
{Body}
Class DerivedClass(BaseClass):
{Body}
self.name = name
std = Student1("johnny")
print(std.getName1(), std.isStudent1())
def getName1(self):
return self.name
class Student1(Child1):
def isStudent1(self):
return True
Programming with Python
R e s o u r c e s a n d Ta s k s t o b e
completed
RESOURCES AND TASKS
Optional / Non-optional Reading resources for the
lecture:
Non-optional Reading resource
Write a Python class to get all possible unique subsets from a set
of distinct integers.
Optional Assignments
Write a Python class named Circle constructed by a radius and two methods
which will compute the area and the perimeter of a circle
RESOURCES AND TASKS
Reading resources for next lecture :
Generators
Iterators
QUIZ TIME
10 MINUTES
“All of you have to give quiz where there will be 8 questions covering part1 and part 2
of lecture. It is compulsory as it is used for assessment and attendance”
QUESTIONS FROM LECTURE 1.1 &
1.2
Which Of The Following Can Be Used To Invoke The __init__ Method In B
From A, Where A Is A Subclass Of B?
A. B.
super().__init__() super().__init__(self)
C. D.
B.__init__() B.__init__(super)
QUESTIONS FROM LECTURE 1.1 &
1.2
Python is fully object-oriented, you can define your own classes,
inherit from your own or built-in classes, and instantiate the classes
you’ve defined. This statement is-
A.
True
B.
False
?
QUESTIONS FROM LECTURE 1.1 &
1.2
_____ represents an entity in the real world with its identity and behaviour.
A. B.
A method An object
C. D.
A class An operator
QUESTIONS FROM LECTURE 1.1 &
1.2
What is Instantiation in terms of OOP terminology?
A. B.
Deleting an instance of Modifying an instance of
class class
C. D.
Copying an instance of Creating an instance of
class class
QUESTIONS FROM LECTURE 1.1 &
1.2
A. B.
itertools.product() re.cartesian()
C. D.
CartesianProduct() itertools.CartesianProduc
t()
QUESTIONS FROM LECTURE 1.1 &
1.2
A. B.
itertools.join() itertools.chain()
C. D.
itertools.product() itertools.group()
QUESTIONS FROM LECTURE 1.1 &
1.2
A.
True
B.
False
?
QUESTIONS FROM LECTURE 1.1 &
1.2
A.
True
B.
False
?
THANK YOU