0% found this document useful (0 votes)
13 views

UNIT 2 Encapsulation PRABHA

The document covers key concepts of Python programming, focusing on encapsulation, inheritance, method overriding, data abstraction, and data hiding. It explains how encapsulation is achieved through access modifiers, the types of inheritance available in Python, and the importance of data abstraction and hiding. Additionally, it discusses the advantages and disadvantages of data hiding in object-oriented programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

UNIT 2 Encapsulation PRABHA

The document covers key concepts of Python programming, focusing on encapsulation, inheritance, method overriding, data abstraction, and data hiding. It explains how encapsulation is achieved through access modifiers, the types of inheritance available in Python, and the importance of data abstraction and hiding. Additionally, it discusses the advantages and disadvantages of data hiding in object-oriented programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Python Programming

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.

Abstract Classes in Python


• Abstract Class: The classes that cannot be instantiated. This means that we cannot create objects of an
abstract class and these are only meant to be inherited. Then an object of the derived class is used to access
the features of the base class. These are specifically defined to lay a foundation of other classes that exhibit
common behavior or characteristics.
• The abstract class is an interface. Interfaces in OOP enable a class to inherit data and functions from a base
class by extending it. In Python, we use the NotImplementedError to restrict the instantiation of a class. Any
class having this error inside method definitions cannot be instantiated.
Consider an example where we create an abstract class Fruit. We derive two classes Mango and Orange from the Fruit
class that implement the methods defined in this class. Here the Fruit class is the parent abstract class and the
classes Mango and Apple become the sub/child classes. We won’t be able to access the methods of the Fruit class by
simply creating an object, we will have to create the objects of the derived classes: Mango and Apple to access the
methods.
An abstract class can have both abstract methods and concrete methods.
We can now access the concrete method of the abstract class by instantiating an object of the child class. We can
also access the abstract methods of the child classes with it. Interesting points to keep in mind are:
•We always need to provide an implementation of the abstract method in the child class even when implementation
is given in the abstract class.
•A subclass must implement all abstract methods that are defined in the parent class otherwise it results in an error.
Why Data Abstraction is Important?

• Data Abstraction firstly saves a lot of our time as we do not have to


repeat the code that may be the same for all the classes.
• Moreover, if there are any additional features, they can be easily
added, thus improving flexibility.
• Not to mention, working in large teams becomes easier as one
won’t have to remember every function and the basic structure
can be inherited without any confusions.
Data Hiding
• Data hiding is a method used in object-oriented programming (OOP) to hide the
details of an object and function. Or in other words, data hiding means isolating
the client from the implementation part of the application.
• In Python, modules are enough to understand how to use a particular application
correctly, but users can’t know how the application is working and what processes
are running inside it.
• Thus, data hiding is the most essential feature because it avoids dependency and
provides security. Data hiding is also known as information hiding or
encapsulation.
Advantages and Disadvantages of Data Hiding
Advantages of Data Hiding
•By data hiding, the objects in the class are disconnected from the irrelevant data.
•It increases the security of the data so that hackers can not access it.
•Data hiding helps to prevent damage to volatile data by hiding it from the public.
Disadvantages of Data Hiding
•For the data hiding, the programmers have to write extra lines of code that become lengthy and
unreadable.
•Data hiding prevents the linkage between visible and invisible data that makes the objects work faster.
•The objects work comparatively slower. That's why object-oriented programs are typically slower than
procedure-based programs.

You might also like