Unit: 4 OOP and File Handling: Prepared By: Ms. Parveen Mor Dahiya
Unit: 4 OOP and File Handling: Prepared By: Ms. Parveen Mor Dahiya
Unit: 4 OOP and File Handling: Prepared By: Ms. Parveen Mor Dahiya
class BookStore:
def __init__(self, attrib1, attrib2):
self.attrib1 = attrib1
self.attrib2 = attrib2
Derived class inherits features from the base class, adding new features
to it. This results into re-usability of code.
Example
Multiple Inheritance in Python
In multiple inheritance, the features of all the base classes are inherited
into the derived class. The syntax for multiple inheritance is similar to
single inheritance.
class Base1:
pass
class Base2:
pass
class MultiDerived(Base1, Base2):
Pass
Here, MultiDerived is derived from classes Base1 and
Base2.
Example:
Multilevel inheritance
In multilevel inheritance, features of the base class and the derived
class is inherited into the new derived class.
class Base:
pass
class Derived1(Base):
pass
class Derived2(Derived1):
pass
Example:
Polymorphism
Polymorphism means having many forms. In programming,
polymorphism means same function name (but different signatures)
being uses for different types.
As we know, a child class inherits all the methods from the parent
class. However, you will encounter situations where the method
inherited from the parent class doesn’t quite fit into the child class. In
such cases, you will have to re-implement method in the child class.
This process is known as Method Overriding.
Polymorphism with Inheritance:
Method Overriding
Method overriding is an ability of any object-oriented programming language
that allows a subclass or child class to provide a specific implementation of a
method that is already provided by one of its super-classes or parent classes.
When a method in a subclass has the same name, same parameters or signature
and same return type(or sub-type) as a method in its super-class, then the
method in the subclass is said to override the method in the super-class.
...
Override means having two methods with the same
name but doing different tasks. It means that one of the
methods overrides the other.
If there is any method in the superclass and a method
with the same name in a subclass, then by executing
the method, the method of the corresponding class will
be executed.
Polymorphism with a Function and
objects:
It is also possible to create a function that can take any object, allowing
for polymorphism. In this example, let’s create a function called
“func()” which will take an object which we will name “obj”.
Though we are using the name ‘obj’, any instantiated object will be
able to be called into this function. Next, lets give the function
something to do that uses the ‘obj’ object we passed to it.
In this case lets call the three methods, viz., capital(), language() and
type(), each of which is defined in the two classes ‘India’ and ‘USA’.
Data abstraction in python
Abstraction is an important aspect of object-oriented
programming. In python, we can also perform data
hiding by adding the double underscore (___) as a
prefix to the attribute which is to be hidden. After this,
the attribute will not be visible outside of the class
through the object.
Example:
Access Modifiers in Python : Public,
Private and Protected
Most programming languages has three forms of access modifiers,
which are Public, Protected and Private in a class.
Python uses ‘_’ symbol to determine the access control for a specific
data member or a member function of a class. Access specifiers in
Python have an important role to play in securing data from
unauthorized access and in preventing it from being exploited.
A Class in Python has three types of access modifiers –
Public Access Modifier
Protected Access Modifier
Private Access Modifier
Introduction
Public Access Modifier:
The members of a class which are declared public are easily accessible
from any part of the program. All data members and member functions of
a class are public by default.
Protected Access Modifier:
The members of a class which are declared protected are only accessible to
a class derived from it. Data members of a class are declared protected by
adding a single underscore ‘_’ symbol before the data member of that
class.
Private Access Modifier:
The members of a class which are declared private are accessible within
the class only, private access modifier is the most secure access modifier.
Data members of a class are declared private by adding a double
underscore ‘__’ symbol before the data member of that class.
Example: public
Example: Protected
Example: Private
Encapsulation
Encapsulation is one of the fundamental concepts in object-oriented
programming (OOP). It describes the idea of wrapping data and the methods that
work on data within one unit.
This puts restrictions on accessing variables and methods directly and can
prevent the accidental modification of data. To prevent accidental change, an
object’s variable can only be changed by an object’s method. Those type of
variables are known as private variable.
A class is an example of encapsulation as it encapsulates all the data that is
member functions, variables, etc.
An objects variables should not always be directly accessible.
To prevent accidental change, an objects variables can sometimes only be
changed with an objects methods. Those type of variables are private variables.
The methods can ensure the correct values are set. If an incorrect value is set, the
method can return an error.
A double underscore: Private variable, harder to access but still possible.
Example
Static Keyword(Class or Static variables )
Class or Static variables are the variables that belong
to the class and not to objects. Class or Static variables
are shared amongst objects of the class.
All variables which are assigned a value in the class
declaration are class variables. And
Variables which are assigned values inside class
methods are instance variables.
Static Methods in Python
Just like static variables, static methods are the methods which are bound to the
class rather than an object of the class and hence are called using the class name
and not the objects of the class.
As static methods are bound to the class hence they cannot change the state of an
object.
To call a static method we don't need any class object it can be directly called using
the class name.