0% found this document useful (0 votes)
10 views5 pages

Python Abstraction and Abstract Classes

The document explains abstraction in Python as a key principle of object-oriented programming that hides unnecessary details to reduce complexity. It discusses two types of abstraction: data abstraction and process abstraction, and introduces the concept of abstract classes, which cannot be instantiated but can serve as base classes. An example is provided to illustrate how to create an abstract class and override its abstract methods in a child class.

Uploaded by

user.dnne12
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)
10 views5 pages

Python Abstraction and Abstract Classes

The document explains abstraction in Python as a key principle of object-oriented programming that hides unnecessary details to reduce complexity. It discusses two types of abstraction: data abstraction and process abstraction, and introduces the concept of abstract classes, which cannot be instantiated but can serve as base classes. An example is provided to illustrate how to create an abstract class and override its abstract methods in a child class.

Uploaded by

user.dnne12
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

Page 1 of 5

Home Whiteboard Online Compilers Practice Articles AI Assistant

SQL HTML CSS Javascript Python Java C C++ PHP Scala C#

Python - Abstraction

Abstraction is one of the important principles of object-oriented programming. It refers


to a programming approach by which only the relevant data about an object is exposed,
hiding all the other details. This approach helps in reducing the complexity and
increasing the efficiency of application development.

Types of Python Abstraction


There are two types of abstraction. One is data abstraction, wherein the original data
entity is hidden via a data structure that can internally work through the hidden data
entities. Another type is called process abstraction. It refers to hiding the underlying
implementation details of a process.

Python Abstract Class


In object-oriented programming terminology, a class is said to be an abstract class if it
cannot be instantiated, that is you can have an object of an abstract class. You can
however use it as a base or parent class for constructing other classes.

Create an Abstract Class


To create an abstract class in Python, it must inherit the ABC class that is defined in the
ABC module. This module is available in Python's standard library. Moreover, the class
must have at least one abstract method. Again, an abstract method is the one which
cannot be called but can be overridden. You need to decorate it with @abstractmethod
decorator.

Example: Create an Absctract Class

from abc import ABC, abstractmethod


class demo(ABC):
Page 2 of 5

@abstractmethod
def method1(self):
print ("abstract method")
return
def method2(self):
print ("concrete method")

The demo class inherits ABC class. There is a method1() which is an abstract method.
Note that the class may have other non-abstract (concrete) methods.

If you try to declare an object of demo class, Python raises TypeError −

obj = demo()
^^^^^^
TypeError: Can't instantiate abstract class demo with abstract method method1

The demo class here may be used as parent for another class. However, the child class
must override the abstract method in parent class. If not, Python throws this error −

TypeError: Can't instantiate abstract class concreteclass with abstract method me

 

Abstract Method Overriding


Hence, the child class with the abstract method overridden is given in the following
example −

Example

Open Compiler

from abc import ABC, abstractmethod


class democlass(ABC):
@abstractmethod
def method1(self):
print ("abstract method")
return
def method2(self):
print ("concrete method")

class concreteclass(democlass):
Page 3 of 5

def method1(self):
super().method1()
return

obj = concreteclass()
obj.method1()
obj.method2()

Output

When you execute this code, it will produce the following output −

abstract method
concrete method

Chapters Categories
TOP TUTORIALS

Python Tutorial
Java Tutorial
C++ Tutorial

C Programming Tutorial
C# Tutorial
PHP Tutorial

R Tutorial

HTML Tutorial
CSS Tutorial

JavaScript Tutorial

SQL Tutorial

TRENDING TECHNOLOGIES

Cloud Computing Tutorial


Amazon Web Services Tutorial

Microsoft Azure Tutorial

Git Tutorial
Ethical Hacking Tutorial
Page 4 of 5

Docker Tutorial

Kubernetes Tutorial

DSA Tutorial
Spring Boot Tutorial

SDLC Tutorial

Unix Tutorial

CERTIFICATIONS

Business Analytics Certification

Java & Spring Boot Advanced Certification


Data Science Advanced Certification

Cloud Computing And DevOps

Advanced Certification In Business Analytics


Artificial Intelligence And Machine Learning

DevOps Certification

Game Development Certification


Front-End Developer Certification

AWS Certification Training

Python Programming Certification

COMPILERS & EDITORS

Online Java Compiler


Online Python Compiler

Online Go Compiler

Online C Compiler
Online C++ Compiler

Online C# Compiler

Online PHP Compiler


Online MATLAB Compiler

Online Bash Terminal

Online SQL Compiler


Online Html Editor

ABOUT US | OUR TEAM | CAREERS | JOBS | CONTACT US | TERMS OF USE |

PRIVACY POLICY | REFUND POLICY | COOKIES POLICY | FAQ'S


Page 5 of 5

Tutorials Point is a leading Ed Tech company striving to provide the best learning material on
technical and non-technical subjects.

© Copyright 2025. All Rights Reserved.

You might also like