Skip to content
Cover image for: Abstraction in Python: A Complete Guide
#pythonIntermediate

Abstraction in Python: A Complete Guide

May 6, 2026
Updated May 14, 2026
30 min read

AI Insights

Powered by GPT-4o-mini

Verified Context: abstraction-in-python-a-complete-guide

Understand abstraction in Python using the abc module and Abstract Base Classes. Learn to define abstract methods with @abstractmethod that enforce implementation contracts in all subclasses, design clean interfaces that separate what from how, create abstract properties and static methods, build extensible class hierarchies, and implement practical patterns with a BankApp abstract class that defines the interface for different account types and transaction processing systems.

Quick Summary

Data abstraction means showing only the essential features and hiding the complex internal details. Technically, in Python abstraction is used to hide the implementation details from the user and expose only necessary parts, making the code simpler and easier to interact with.

Abstraction

Data abstraction means showing only the essential features and hiding the complex internal details. Technically, in Python abstraction is used to hide the implementation details from the user and expose only necessary parts, making the code simpler and easier to interact with.

Defining an abstract class with a mandatory abstract method in Python

Explanation

  • The code imports the ABC and abstractmethod from the abc module to create an abstract base class.
  • The BankApp class inherits from ABC, making it an abstract class that cannot be instantiated directly.
  • The database method is a concrete method that prints a message indicating a connection to a database.
  • The security method is defined as an abstract method, requiring any subclass to implement this method without providing any implementation in the base class.
  • This structure enforces a contract for subclasses, ensuring they provide specific functionality while allowing shared behavior through concrete methods.
python

This code defines a MobileApp class that inherits from BankApp and includes a method for mobile login.

Explanation

  • The MobileApp class is a subclass of BankApp, indicating it inherits properties and methods from the BankApp class.
  • The mobile_login method, when called, prints the message 'login into mobile' to the console.
  • This method can be used to implement mobile-specific login functionality within the context of a banking application.
  • The use of inheritance allows for code reuse and organization, making it easier to manage related functionalities.
python

This code snippet demonstrates the instantiation of a MobileApp class and emphasizes the need for abstract methods.

Explanation

  • The variable mob is created as an instance of the MobileApp class.
  • The comment indicates that the class likely contains abstract methods that must be implemented in subclasses.
  • Abstract methods are defined in a base class but are intended to be overridden in derived classes, enforcing a contract for subclasses.
  • This structure is commonly used in object-oriented programming to ensure that certain methods are implemented in any subclass of the abstract class.
python

Output

text

This code defines a MobileApp class that inherits from BankApp and includes methods for mobile login and security features.

Explanation

  • The MobileApp class is a subclass of BankApp, indicating it inherits properties and methods from the BankApp class.
  • The mobile_login method prints a message indicating that the user is logging into the mobile application.
  • The security method prints a message related to mobile security, suggesting it may handle security features specific to mobile access.
  • This structure allows for extending functionality specific to mobile applications while maintaining a connection to the base banking application.
python

Creating a mobile application instance with the MobileApp class

Explanation

  • Initializes a new instance of the MobileApp class to represent a mobile application
  • The variable mob serves as a reference point for interacting with the mobile app functionality
  • This object-oriented approach allows for encapsulation of mobile app related methods and properties
  • The instantiation follows standard Python conventions for creating class instances
  • This setup enables subsequent operations on the mobile application through the mob reference
python

Explanation

  • Calls the security() method on the mob object, which likely handles security functionalities.
  • The method may include tasks such as authentication, authorization, or encryption processes.
  • The specific implementation details of the security() method are not provided, so its behavior depends on the mob class definition.
  • This code assumes that the mob object is already instantiated and properly configured before this method call.
python

Output

text

This code snippet initializes the database connection for the 'mob' object.

Explanation

  • Calls the database() method on the mob object, which is likely part of a larger framework or library.
  • Establishes a connection to a database, enabling data retrieval and manipulation.
  • The method may handle configuration settings such as database type, credentials, and connection parameters.
  • Essential for performing database operations like queries, inserts, updates, and deletes within the application.
python

Output

text

Problem-1: Class inheritence

Create a Bus child class that inherits from the Vehicle class. The default fare charge of any vehicle is seating capacity * 100. If Vehicle is Bus instance, we need to add an extra 10% on full fare as a maintenance charge. So total fare for bus instance will become the final amount = total fare + 10% of the total fare.

Note: The bus seating capacity is 50. so the final fare amount should be 5500. You need to override the fare() method of a Vehicle class in Bus class.

This code defines a vehicle class hierarchy with fare calculation for a bus.

Explanation

  • The Vehicle class initializes with type and capacity attributes and has a method fare that calculates the fare based on capacity.
  • The Bus class inherits from Vehicle and overrides the fare method to include an additional 10% surcharge on the base fare.
  • An instance of Bus is created with a type of 'school bus' and a capacity of 50.
  • The fare method of the Bus instance is called and prints the total fare, which includes the surcharge.
python

Output

text

Problem-3: Write a program that has a class Point. Define another class Location which has two objects (Location & Destination) of class Point. Also define a function in Location that prints the reflection of Destination on the x axis.

This code defines a structure for representing points and their reflections in a 2D space.

Explanation

  • The Point class initializes a point with x and y coordinates and has a method to display its coordinates.
  • The Location class contains two Point instances, representing a source and a destination, initialized with given coordinates.
  • The show method in the Location class prints the coordinates of both the source and destination points.
  • The reflection method modifies the y-coordinate of the destination point to its negative value, effectively reflecting it across the x-axis, and prints the new coordinates.
  • An instance of Location is created with specified coordinates, and both the show and reflection methods are called to demonstrate functionality.
python

Output

text

Problem-4: Write a program that has an abstract class Polygon. Derive two classes Rectangle and Triamgle from Polygon and write methods to get the details of their dimensions and hence calculate the area.

Implementing an abstract base class for geometric shapes in Python

Explanation

  • Defines an abstract base class Polygon with two abstract methods: get_data and area, enforcing a structure for derived classes.
  • Implements the Rectangle class that inherits from Polygon, providing specific implementations for get_data and area methods to calculate the area of a rectangle.
  • Implements the Triangle class similarly, allowing for the calculation of the area of a triangle using its base and height.
  • Creates instances of Rectangle and Triangle, sets their dimensions using get_data, and calculates their areas, demonstrating polymorphism in action.
python

Output

text

Problem-5: Write a program with class Bill. The users have the option to pay the bill either by cheque or by cash. Use the inheritance to model this situation.

Implementing a billing system with cash and cheque payment options in Python

Explanation

  • Defines a Bill class that calculates the total price of items and displays them.
  • The CashPayment class inherits from Bill, adding functionality to show cash payment details, including denominations and their values.
  • The ChequePayment class also inherits from Bill, providing methods to display cheque payment information, such as cheque number and bank name.
  • Each payment class utilizes the super() function to access the parent class's methods for displaying item details.
python

This code snippet demonstrates how to create and display a cash payment system for various items.

Explanation

  • A list of items and their corresponding prices is defined, representing products available for purchase.
  • Denominations of currency notes and their respective values are specified in two separate lists.
  • An instance of the CashPayment class is created using the items, prices, denominations, and values.
  • The show_cash_payment method is called on the cash object to display the payment details.
python

Output

text

This code snippet facilitates payment processing through cheque or cash for a list of items.

Explanation

  • A list of items and their corresponding prices is defined for payment options.
  • The user is prompted to choose between cheque (option 1) or cash (option 2) for payment.
  • If the cheque option is selected, the user must provide bank details and a cheque number, which are then used to create a ChequePayment object.
  • If the cash option is selected, predefined denominations and their quantities are used to create a CashPayment object.
  • Each payment method has a method to display the payment details to the user.
python

Output

text

Next in this series: Python File Handling: Read, Write, Serialize & Deserialize →

Frequently Asked Questions

Data abstraction in Python means showing only the essential features and hiding the complex internal details, making the code simpler and easier to interact with.
An abstract class in Python is defined by importing ABC and abstractmethod from the abc module, and it must have at least one abstract method.
The security method is defined as an abstract method, requiring any subclass to implement this method without providing any implementation in the base class.
The MobileApp class cannot be instantiated directly because it inherits from an abstract class and must implement the abstract method 'security'.
The mobilelogin method, when called, prints the message 'login into mobile' to the console, implementing mobile-specific login functionality within a banking application.

How was this tutorial?

Abstraction in Python: A Complete Guide | Madhu Dadi