UNIT 2 Encapsulation PRABHA
UNIT 2 Encapsulation PRABHA
Topics covered:
Encapsulation, Inheritance, Method Overriding, Data Abstraction, Data Hiding
Prepared by
Dr. Shanmuga Prabha P
Assistant Professor
Encapsulation
Encapsulation
Example:
In this example, we create an Employee class by defining employee attributes such
as name and salary as an instance variable and implementing behavior
using work() and show() instance methods.
Access Modifiers in
Python
Encapsulation can be achieved by declaring the data members and methods of a class
either as private or protected. But In Python, we don’t have direct access modifiers like
public, private, and protected. We can achieve this by using
single underscore and double underscores.
Access modifiers limit access to the variables and methods of a class. Python provides three
types of access modifiers private, public, and protected.
Inheritance in Python
• The process of inheriting the properties of the parent class into a child
class is called inheritance. The existing class is called a base class or parent
class and the new class is called a subclass or child class or derived class.
• In this Python lesson, you will learn inheritance, method overloading, method
overriding, types of inheritance, and MRO (Method Resolution Order).
• In Object-oriented programming, inheritance is an important aspect. The main
purpose of inheritance is the reusability of code because we can use the
existing class to create a new class instead of creating it from scratch.
• In inheritance, the child class acquires all the data members, properties, and
functions from the parent class. Also, a child class can also provide its specific
implementation to the methods of the parent class.
For example, In the real world, Car is a sub-class of a Vehicle class. We can create a Car
by inheriting the properties of a Vehicle such as Wheels, Colors, Fuel tank, engine, and add
extra properties in Car as required.
Types Of Inheritance
In Python, based upon the number of child and parent classes involved, there are five
types of inheritance. The type of inheritance are listed below:
1.Single inheritance
2.Multiple Inheritance
3.Multilevel inheritance
4.Hierarchical Inheritance
5.Hybrid Inheritance
Multiple Inheritance
In multiple inheritance, one child class can
inherit from multiple parent classes. So
here is one child class and multiple parent
classes.
Multilevel inheritance
In multilevel inheritance, a class inherits from a
child class or derived class. Suppose three classes
A, B, C. A is the superclass, B is the child class of A,
C is the child class of B. In other words, we can say
a chain of classes is called multilevel
inheritance
Hierarchical Inheritance
In Hierarchical inheritance, more than one child
class is derived from a single parent class. In other
words, we can say one parent class and multiple
child classes.
Hybrid Inheritance
When inheritance is consists of multiple types or a
combination of different inheritance is called
hybrid inheritance.
Python super() function
When a class inherits all properties and behavior from the parent class
is called inheritance. In such a case, the inherited class is a subclass
and the latter class is the parent class.
In child class, we can refer to parent class by using
the super() function. The super function returns a temporary object of
the parent class that allows us to call a parent class method inside a
child class method.
Benefits of using the super() function.
1.We are not required to remember or specify the parent class name
to access its methods.
2.We can use the super() function in both single and multiple
inheritances.
3.The super() function support code reusability as there is no need
to write the entire function
issubclass()
In Python, we can verify whether a
particular class is a subclass of another
class. For this purpose, we can use Python
built-in function issubclass(). This
function returns True if the given class is
the subclass of the specified class.
Otherwise, it returns False.
Syntax
issubclass(class, classinfo)
Method Overriding
In inheritance, all members available in the parent class are by default available in the child
class. If the child class does not satisfy with parent class implementation, then the child class
is allowed to redefine that method by extending additional functions in the child class. This
concept is called method overriding.
When a child class method has the same name, same parameters, and same return type as
a method in its superclass, then the method in the child is said to override the method in
the parent class.
DATA ABSTRACTION
• The process by which data and functions are defined in such a way that only essential details can be seen
and unnecessary implementations are hidden is called Data Abstraction.
• The main focus of data abstraction is to separate the interface and the implementation of the program.