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

OOP in Python 2 Lec 6

This document discusses abstraction in object oriented programming using Python. It defines abstraction as hiding internal functionality from users while allowing them to interact with basic implementation. An abstract class acts as a blueprint for other classes, containing abstract methods that must be implemented in child classes. The document provides an example of an abstract class for vehicles that is inherited by specific vehicle types. It explains that abstraction allows breaking down complex systems into more manageable hierarchical pieces and provides a common interface for different implementations of a component.

Uploaded by

siddharth malang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

OOP in Python 2 Lec 6

This document discusses abstraction in object oriented programming using Python. It defines abstraction as hiding internal functionality from users while allowing them to interact with basic implementation. An abstract class acts as a blueprint for other classes, containing abstract methods that must be implemented in child classes. The document provides an example of an abstract class for vehicles that is inherited by specific vehicle types. It explains that abstraction allows breaking down complex systems into more manageable hierarchical pieces and provides a common interface for different implementations of a component.

Uploaded by

siddharth malang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Object Oriented Programming

in Python(2)

Lecture 6

Abstraction

Contents
•Abstraction definition
•Abstraction in Deep
•Use Abstract Base classes (ABC)
•Concrete Vs. Abstract (Classes & Methods)
•Example
•When? Why? Using Abstraction
•General Notes

University of Ninevah, IT Collage , SW dept 2


Abstraction definition
• Abstraction is used to hide the internal functionality of the function from the users. The users only
interact with the basic implementation of the function, but inner working is hidden. User is familiar
with that "what function does" but they don't know "how it does.“

• An abstract class can be considered as a blueprint for other classes. It allows you to create a set of
methods that must be created within any child classes built from the abstract class.
• A class which contains one or more abstract methods is called an abstract class.
• An abstract method is a method that has a declaration but does not have an implementation.
• While we are designing large functional units we use an abstract class. When we want to provide a
common interface for different implementations of a component, we use an abstract class.

University of Ninevah, IT Collage , SW dept 3

Abstraction in Deep
• Car Speed

Eng. Size

Wheels

Eng. Size

Weight

University of Ninevah, IT Collage , SW dept 4


Use Abstract Base classes (ABC)
• By default, Python does not provide abstract classes. Python comes with a module that provides the
base for defining Abstract Base classes(ABC) and that module name is ABC. ABC works by decorating
methods of the base class as abstract and then registering concrete classes as implementations of the
abstract base. A method becomes abstract when decorated with the keyword @abstractmethod.

University of Ninevah, IT Collage , SW dept 5

Concrete Vs. Abstract (Classes & Methods)


• Concrete classes contain only concrete (normal)methods whereas abstract classes may
contain both concrete methods and abstract methods.
• The concrete class provides an implementation of abstract methods, the abstract base
class can also provide an implementation by invoking the methods via super().

• Abstract classes are incomplete because they have methods that have nobody. If
python allows creating an object for abstract classes then using that object if anyone
calls the abstract method, but there is no actual implementation to invoke. So we use
an abstract class as a template and according to the need, we extend it and build on it
before we can use it. Due to the fact, an abstract class is not a concrete class, it cannot
be instantiated. When we create an object for the abstract class it raises an error.

University of Ninevah, IT Collage , SW dept 6


Example

University of Ninevah, IT Collage , SW dept 7

When? Why? Using Abstraction


• A powerful way to manage abstraction is through the use of hierarchical
classification.
• This allows us to layer the semantics of complex systems, breaking them into more
manageable pieces.
• From the outside, a car is a single object. Once inside, you see that the car consists
of several subsystems: steering, brakes, sound system, seat belts, etc. In turn, each of
these subsystems is made up of smaller units.

University of Ninevah, IT Collage , SW dept 8


General Notes
the points which we should remember about the abstract base class in Python.
•An Abstract class can contain the both method normal and abstract method.
•An Abstract cannot be instantiated; we cannot create objects for the abstract class.
Abstraction is essential to hide the core functionality from the users. We have covered
the all the basic concepts of Abstraction in Python.

University of Ninevah, IT Collage , SW dept 9

You might also like