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

OOPs_Data_abstraction

The document discusses data abstraction, encapsulation, and polymorphism in Python, highlighting how abstract classes and methods are used to hide implementation details while exposing essential functionalities. It explains the concepts of encapsulation and the importance of protecting data through private and protected members, as well as the two types of polymorphism: compile-time and runtime. Additionally, it contrasts structured programming with object-oriented programming, emphasizing the benefits of the latter in terms of flexibility, abstraction, and code reuse.

Uploaded by

sadikjr330
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

OOPs_Data_abstraction

The document discusses data abstraction, encapsulation, and polymorphism in Python, highlighting how abstract classes and methods are used to hide implementation details while exposing essential functionalities. It explains the concepts of encapsulation and the importance of protecting data through private and protected members, as well as the two types of polymorphism: compile-time and runtime. Additionally, it contrasts structured programming with object-oriented programming, emphasizing the benefits of the latter in terms of flexibility, abstraction, and code reuse.

Uploaded by

sadikjr330
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Data Abstraction

Data Abstraction
• Data abstraction in Python is a programming concept that hides
complex implementation details while exposing only essential
information and functionalities to users.
• In Python, we can achieve data abstraction by using abstract classes
and abstract classes can be created using abc (abstract base class)
module and abstractmethod of abc module.
Abstraction classes in Python
• Abstract class is a class in which one or more abstract methods are
defined.
• When a method is declared inside the class without its
implementation is known as abstract method.
• To create abstract method and abstract classes we have to import the
“ABC” and “abstractmethod” classes from abc (Abstract Base Class)
library.
• Abstract method of base class force its child class to write the
implementation of the all abstract methods defined in base class.
• If we do not implement the abstract methods of base class in the
child class then our code will give error.
In the given code
method_1 is a
abstract method
created using
@abstractmethod
decorator.
Concrete Method:
• Concrete methods are the methods defined in an abstract base class
with their complete implementation.
• Concrete methods are required to avoid reprication of code in
subclasses. For example, in abstract base class there may be a
method that implementation is to be same in all its subclasses, so we
write the implementation of that method in abstract base class after
which we do not need to write implementation of the concrete
method again and again in every subclass.
In the given
code
startEngine
is a concrete
method.
Steps to Create Abstract Base
Class and Abstract Method:
1. Firstly, we import ABC and abstractmethod class from abc (Abstract
Base Class) library.
2. Create a BaseClass that inherits from the ABC class. In Python,
when a class inherits from ABC, it indicates that the class is
intended to be an abstract base class.
Steps to Create Abstract Base
Class and Abstract Method: Cont…
3. Inside BaseClass we declare an abstract method named
“method_1” by using “abstractmethod” decorater. Any subclass
derived from BaseClass must implement this method_1 method.
We write pass in this method which indicates that there is no code
or logic in this method.
Implementation
of Data
Abstraction
Implementation
of Data
Abstraction

Output
Implementation of Data Abstraction
• we have implemented data abstraction using abstract class and
method.
• Firstly, we import the required modules or classes from abc library
then we create a base class ‘Car’ that inherited from ‘ABC’ class that
we have imported.
• Inside base class we create init function, abstract function and non-
abstract functions.
• To declare abstract function printDetails we use “@abstractmethod”
decorator.
Implementation of Data Abstraction
• After that we create child class hatchback and suv.
• Since, these child classes inherited from abstract class so, we need to
write the implementation of all abstract function declared in the base
class.
• We write the implementation of abstract method in both child class.
• We create an instance of a child class and call the printDetails
method.
Encapsulation
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 types of variables are known
as private variables.
Encapsulation
• A class is an example of encapsulation as it encapsulates all the data
that is member functions, variables, etc.
• The goal of information hiding is to ensure that an object’s state is
always valid by controlling access to attributes that are hidden from
the outside world.
• Consider a real-life example of encapsulation, in a company, there are
different sections like the accounts section, finance section, sales
section etc.
• The finance section handles all the financial transactions and keeps
records of all the data related to finance. Similarly, the sales section
handles all the sales-related activities and keeps records of all the
sales.
• Now there may arise a situation when due to some reason an official
from the finance section needs all the data about sales in a particular
month.
• In this case, he is not allowed to directly access the data of the sales
section. He will first have to contact some other officer in the sales
section and then request him to give the particular data.
• This is what encapsulation is.
• Here the data of the sales section and the employees that can
manipulate them are wrapped under a single name “sales section”.
Protected members
• Protected members (in C++ and JAVA) are those members of the class
that cannot be accessed outside the class but can be accessed from
within the class and its subclasses. To accomplish this in Python, just
follow the convention by prefixing the name of the member by a
single underscore “_”.
Protected
members
Protected
members
Private members
• Private members are similar to protected members, the difference is
that the class members declared private should neither be accessed
outside the class nor by any base class.
• to define a private member prefix the member name with double
underscore “__”.
Private
members
Private
members
Polymorphism
Polymorphism
• The word polymorphism means having
many forms. In programming,
polymorphism means the same function
name (but different signatures) being used
for different types. The key difference is the
data types and number of arguments used
in function.
Polymorphism
• Polymorphism in Python refers to the ability of a single function,
method, or operator to work with different types of data or objects.
• It allows objects of different types to be treated as objects of a
common base type.

• There are two main types of polymorphism in Python:


1. Compile-time Polymorphism (Method Overloading)
2. Runtime Polymorphism (Method Overriding).
Compile-Time Polymorphism (Method
Overloading):
• Method overloading is achieved by defining
multiple methods with the same name in
the same class.
• The methods must differ in the type or
number of their parameters.
• The appropriate method is chosen at
compile time based on the number and
types of arguments provided.
Runtime Polymorphism (Method
Overriding):
• Inheritance is used to create a relationship between a base class (or
superclass) and a derived class (or subclass).
• The derived class provides a specific implementation for a method
that is already defined in its base class.
Sr. No. Key Compile-time polymorphism Runtime polymorphism

1 Basic Compile time polymorphism Run time polymorphism where at run time we
means binding is occurring at came to know which method is going to invoke
compile time

2 Static/Dynamic Binding It can be achieved through static It can be achieved through dynamic binding
binding

3 Inheritance Inheritance is not involved Inheritance is involved

4 Example Method overloading is an example Method overriding is an example of runtime


of compile time polymorphism polymorphism
Structured Programming Object-Oriented Programming
It is a subset of procedural programming. It relies on concept of objects that contain data and code.

Programs are divided into small programs or functions. Programs are divided into objects or entities.

It is all about facilitating creation of programs with readable code and reusable components. It is all about creating objects that usually contain both functions and data.

Its main aim is to improve and increase quality, clarity, and development time of computer Its main aim is to improve and increase both quality and productivity of system analysis and
program. design.

It simply focuses on functions and processes that usually work on data. It simply focuses on representing both structure and behavior of information system into tiny or
small modules that generally combines data and process both.

It is a method of organizing, managing and coding programs that can give or provide much It is a method in which set of objects can vary dynamically and can execute just by acting and
easier modification and understanding. reading to each other.

In this, methods are written globally and code lines are processed one by one i.e., Run In this, methods are written globally and code lines are processed one by one i.e., Run
sequentially. sequentially.

It generally follows “Top-Down Approach”. It generally follows “Bottom-Up Approach”.

It provides less flexibility and abstraction as compared to object-oriented programming. It provides more flexibility and abstraction as compared to structured programming.

It is more difficult to modify structured program and reuse code as compared to object- It is less difficult to modify object-oriented programs and reuse code as compared to structured
oriented programs. programs.

It gives more importance of code. It gives more importance to data.

You might also like