Data Abstraction in Python Last Updated : 17 Mar, 2025 Comments Improve Suggest changes Like Article Like Report Data abstraction is one of the most essential concepts of Python OOPs which is used to hide irrelevant details from the user and show the details that are relevant to the users. For example, the readers of geeksforgeeks only know that a writer can write an article on geeksforgeeks, and when it gets published readers can read the articles but the reader is not aware of the background process of publishing the article. A simple example of this can be a car. A car has an accelerator, clutch, and break and we all know that pressing an accelerator will increase the speed of the car and applying the brake can stop the car but we don't know the internal mechanism of the car and how these functionalities can work this detail hiding is known as data abstraction.To understand data abstraction we should be aware of the below basic concepts:OOP concepts in PythonClasses in PythonAbstract classes in PythonImportance of Data AbstractionIt enables programmers to hide complex implementation details while just showing users the most crucial data and functions. This abstraction makes it easier to design modular and well-organized code, makes it simpler to understand and maintain, promotes code reuse, and improves developer collaboration.Data Abstraction in PythonData 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 PythonAbstract 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. Abstract Method: In Python, abstract method feature is not a default feature. 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 below code method_1 is a abstract method created using @abstractmethod decorator.from abc import ABC, abstractmethodclass BaseClass(ABC): @abstractmethod def method_1(self): #empty body passConcrete Method: Concrete methods are the methods defined in an abstract base class with their complete implementation. Concrete methods are required to avoid replication 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 below code startEngine is a concrete method.class Car(ABC): def __init__(self, brand, model, year): self.brand = brand self.model = model self.year = year self.engine_started = True def startEngine(self): if not self.engine_started: print(f"Starting the {self.model}'s engine.") self.engine_started = True else: print("Engine is already running.")Steps to Create Abstract Base Class and Abstract Method:Firstly, we import ABC and abstractmethod class from abc (Abstract Base Class) library. 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. 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.from abc import ABC, abstractmethodclass BaseClass(ABC): @abstractmethod def method_1(self): #empty body passImplementation of Data Abstraction in PythonIn the below code, 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. 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. In this way we can achieve the data abstraction. Python # Import required modules from abc import ABC, abstractmethod # Create Abstract base class class Car(ABC): def __init__(self, brand, model, year): self.brand = brand self.model = model self.year = year # Create abstract method @abstractmethod def printDetails(self): pass # Create concrete method def accelerate(self): print("Speed up ...") def break_applied(self): print("Car stopped") # Create a child class class Hatchback(Car): def printDetails(self): print("Brand:", self.brand) print("Model:", self.model) print("Year:", self.year) def sunroof(self): print("Not having this feature") # Create a child class class Suv(Car): def printDetails(self): print("Brand:", self.brand) print("Model:", self.model) print("Year:", self.year) def sunroof(self): print("Available") # Create an instance of the Hatchback class car1 = Hatchback("Maruti", "Alto", "2022") # Call methods car1.printDetails() car1.accelerate() car1.sunroof() OutputBrand: Maruti Model: Alto Year: 2022 Speed up ... Not having this feature Comment More infoAdvertise with us Next Article Abstract Classes in Python sagar99 Follow Improve Article Tags : Python python-oop-concepts Python-OOP Practice Tags : python Similar Reads Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read Python Classes and Objects A class in Python is a user-defined template for creating objects. It bundles data and functions together, making it easier to manage and use them. When we create a new class, we define a new type of object. We can then create multiple instances of this object type.Classes are created using class ke 6 min read Python objects A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to 2 min read Class and Objectself in Python classIn Python, self is a fundamental concept when working with object-oriented programming (OOP). It represents the instance of the class being used. Whenever we create an object from a class, self refers to the current object instance. It is essential for accessing attributes and methods within the cla 6 min read Class and Instance Attributes in PythonClass attributes: Class attributes belong to the class itself they will be shared by all the instances. Such attributes are defined in the class body parts usually at the top, for legibility. Python # Write Python code here class sampleclass: count = 0 # class attribute def increase(self): samplecla 2 min read Create a Python SubclassIn Python, a subclass is a class that inherits attributes and methods from another class, known as the superclass or parent class. When you create a subclass, it can reuse and extend the functionality of the superclass. This allows you to create specialized versions of existing classes without havin 3 min read Inner Class in PythonPython is an Object-Oriented Programming Language, everything in Python is related to objects, methods, and properties. A class is a user-defined blueprint or a prototype, which we can use to create the objects of a class. The class is defined by using the class keyword.Example of classPython# creat 5 min read Python MetaClassesThe key concept of python is objects. Almost everything in python is an object, which includes functions and as well as classes. As a result, functions and classes can be passed as arguments, can exist as an instance, and so on. Above all, the concept of objects let the classes in generating other c 9 min read Creating Instance Objects in PythonIn Python, an instance object is an individual object created from a class, which serves as a blueprint defining the attributes (data) and methods (functions) for the object. When we create an object from a class, it is referred to as an instance. Each instance has its own unique data but shares the 3 min read Dynamic Attributes in PythonDynamic attributes in Python are terminologies for attributes that are defined at runtime, after creating the objects or instances. In Python we call all functions, methods also as an object. So you can define a dynamic instance attribute for nearly anything in Python. Consider the below example for 2 min read Constructors in PythonIn Python, a constructor is a special method that is called automatically when an object is created from a class. Its main role is to initialize the object by setting up its attributes or state. The method __new__ is the constructor that creates a new instance of the class while __init__ is the init 3 min read Why Python Uses 'Self' as Default ArgumentIn Python, when defining methods within a class, the first parameter is always self. The parameter self is a convention not a keyword and it plays a key role in Pythonâs object-oriented structure.Example:Pythonclass Car: def __init__(self, brand, model): self.brand = brand # Set instance attribute s 3 min read Encapsulation and Access ModifiersEncapsulation in PythonIn Python, encapsulation refers to the bundling of data (attributes) and methods (functions) that operate on the data into a single unit, typically a class. It also restricts direct access to some components, which helps protect the integrity of the data and ensures proper usage.Table of ContentEnca 6 min read Access Modifiers in Python : Public, Private and ProtectedPrerequisites: Underscore (_) in Python, Private Variables in PythonEncapsulation is one of the four principles used in Object Oriented Paradigm. It is used to bind and hide data to the class. Data hiding is also referred as Scoping and the accessibility of a method or a field of a class can be chan 9 min read Access Modifiers in Python : Public, Private and ProtectedPrerequisites: Underscore (_) in Python, Private Variables in PythonEncapsulation is one of the four principles used in Object Oriented Paradigm. It is used to bind and hide data to the class. Data hiding is also referred as Scoping and the accessibility of a method or a field of a class can be chan 9 min read Private Variables in PythonPrerequisite: Underscore in PythonIn Python, there is no existence of âPrivateâ instance variables that cannot be accessed except inside an object. However, a convention is being followed by most Python code and coders i.e., a name prefixed with an underscore, For e.g. _geek should be treated as a n 3 min read Private Methods in PythonEncapsulation is one of the fundamental concepts in object-oriented programming (OOP) in Python. 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 6 min read Protected variable in PythonPrerequisites: Underscore ( _ ) in Python A Variable is an identifier that we assign to a memory location which is used to hold values in a computer program. Variables are named locations of storage in the program. Based on access specification, variables can be public, protected and private in a cl 2 min read InheritanceInheritance in PythonInheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (called a child or derived class) to inherit attributes and methods from another class (called a parent or base class). This promotes code reuse, modularity, and a hierarchical class structure. In this arti 7 min read Method Overriding in PythonMethod overriding is an ability of any object-oriented programming language that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, the same parameter 7 min read Operator Overloading in PythonOperator Overloading means giving extended meaning beyond their predefined operational meaning. For example operator + is used to add two integers as well as join two strings and merge two lists. It is achievable because '+' operator is overloaded by int class and str class. You might have noticed t 8 min read Python super()In Python, the super() function is used to refer to the parent class or superclass. It allows you to call methods defined in the superclass from the subclass, enabling you to extend and customize the functionality inherited from the parent class.Syntax of super() in PythonSyntax: super()Return : Ret 8 min read Multiple Inheritance in PythonInheritance is the mechanism to achieve the re-usability of code as one class(child class) can derive the properties of another class(parent class). It also provides transitivity ie. if class C inherits from P then all the sub-classes of C would also inherit from P. Multiple Inheritance When a class 5 min read What Is Hybrid Inheritance In Python?Inheritance is a fundamental concept in object-oriented programming (OOP) where a class can inherit attributes and methods from another class. Hybrid inheritance is a combination of more than one type of inheritance. In this article, we will learn about hybrid inheritance in Python. Hybrid Inheritan 3 min read Multilevel Inheritance in PythonPython is one of the most popular and widely used Programming Languages. Python is an Object Oriented Programming language which means it has features like Inheritance, Encapsulation, Polymorphism, and Abstraction. In this article, we are going to learn about Multilevel Inheritance in Python. Pre-Re 3 min read Multilevel Inheritance in PythonPython is one of the most popular and widely used Programming Languages. Python is an Object Oriented Programming language which means it has features like Inheritance, Encapsulation, Polymorphism, and Abstraction. In this article, we are going to learn about Multilevel Inheritance in Python. Pre-Re 3 min read PolymorphismPolymorphism in PythonPolymorphism is a foundational concept in programming that allows entities like functions, methods or operators to behave differently based on the type of data they are handling. Derived from Greek, the term literally means "many forms".Python's dynamic typing and duck typing make it inherently poly 6 min read Python | Method OverloadingMethod Overloading:Two or more methods have the same name but different numbers of parameters or different types of parameters, or both. These methods are called overloaded methods and this is called method overloading. Like other languages (for example, method overloading in C++) do, python does no 7 min read Method Overriding in PythonMethod overriding is an ability of any object-oriented programming language that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, the same parameter 7 min read AbstractionData Abstraction in PythonData abstraction is one of the most essential concepts of Python OOPs which is used to hide irrelevant details from the user and show the details that are relevant to the users. For example, the readers of geeksforgeeks only know that a writer can write an article on geeksforgeeks, and when it gets 5 min read Abstract Classes in PythonIn Python, an abstract class is a class that cannot be instantiated on its own and is designed to be a blueprint for other classes. Abstract classes allow us to define methods that must be implemented by subclasses, ensuring a consistent interface while still allowing the subclasses to provide speci 5 min read Python-interface moduleIn object-oriented languages like Python, the interface is a collection of method signatures that should be provided by the implementing class. Implementing an interface is a way of writing an organized code and achieve abstraction. The package zope.interface provides an implementation of "object in 3 min read Difference between abstract class and interface in PythonIn this article, we are going to see the difference between abstract classes and interface in Python, Below are the points that are discussed in this article: What is an abstract class in Python?What is an interface in Python?Difference between abstract class and interface in PythonWhat is an Abstra 4 min read Special Methods and TestingDunder or magic methods in PythonPython Magic methods are the methods starting and ending with double underscores '__'. They are defined by built-in classes in Python and commonly used for operator overloading. They are also called Dunder methods, Dunder here means "Double Under (Underscores)". Python Magic MethodsBuilt in classes 7 min read __init__ in PythonPrerequisites - Python Class and Objects, Self__init__ method in Python is used to initialize objects of a class. It is also called a constructor. It is like a default constructor in C++ and Java. Constructors are used to initialize the objectâs state.The task of constructors is to initialize (assig 5 min read Object oriented testing in PythonPrerequisite: Object-Oriented Testing Automated Object-Oriented Testing can be performed in Python using Pytest testing tool. In this article, we perform object-oriented testing by executing test cases for classes. We write a program that has one parent class called Product and three child classes - 5 min read Additional ResourcesObject oriented testing in PythonPrerequisite: Object-Oriented Testing Automated Object-Oriented Testing can be performed in Python using Pytest testing tool. In this article, we perform object-oriented testing by executing test cases for classes. We write a program that has one parent class called Product and three child classes - 5 min read classmethod() in PythonThe classmethod() is an inbuilt function in Python, which returns a class method for a given function. This means that classmethod() is a built-in Python function that transforms a regular method into a class method. When a method is defined using the @classmethod decorator (which internally calls c 8 min read Decorators in PythonIn Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are 10 min read Destructors in PythonConstructors in PythonDestructors are called when an object gets destroyed. In Python, destructors are not needed as much as in C++ because Python has a garbage collector that handles memory management automatically. The __del__() method is a known as a destructor method in Python. It is called when 7 min read 8 Tips For Object-Oriented Programming in PythonOOP or Object-Oriented Programming is a programming paradigm that organizes software design around data or objects and relies on the concept of classes and objects, rather than functions and logic. Object-oriented programming ensures code reusability and prevents Redundancy, and hence has become ver 6 min read Like