INHERITANCE
in
y.
IN
ud
st
PYTHON
ty
si
er
iv
un
un
iv
er
si
ty
st
ud
y.
in
• Instead of starting from scratch, you can
in
create a class by deriving it from a preexisting
y.
class by listing the parent class in parentheses
ud
after the new class name.
st
ty
• The child class inherits the attributes of its
si
parent class, and you can use those attributes
er
iv
as if they were defined in the child class. A
un
child class can also override data members
and methods from the parent.
• The Syntax is:
un
iv
er
si
ty
st
ud
y.
in
un
iv
er
si
ty
st
ud
y.
in
un
iv
er
si
ty
st
ud
y.
in
Overriding Methods
• You can always override your parent class
in
methods. One reason for overriding parent's
y.
ud
methods is because you may want special or
st
different functionality in your subclass.
ty
si
er
iv
un
un
iv
er
si
ty
st
ud
Example
y.
in
Base Overloading Methods
in
y.
ud
st
ty
si
er
iv
un
Overloading Operators
• Suppose you have created a Vector class to
in
represent two-dimensional vectors, what
y.
ud
happens when you use the plus operator to
st
add them? Most likely Python will yell at you.
ty
• You could, however, define
er
si
the __add__ method in your class to perform
iv
un
vector addition and then the plus operator
would behave as per expectation
un
iv
er
si
ty
st
ud
Example
y.
in
Data Hiding
• An object's attributes may or may not be
in
visible outside the class definition. You need
y.
ud
to name attributes with a double underscore
st
prefix, and those attributes then are not be
ty
directly visible to outsiders.
si
er
iv
un
un
iv
er
si
ty
st
ud
y.
in
un
iv
er
si
ty
st
ud
y.
in