Table of Contents
ToggleAbstraction in Python
Abstraction in Python Programming is all about hiding unnecessary data and showing only what is necessary. Imagine you are a front-end developer making a website. You need a button to submit a form. Instead of writing the lengthy code every time, you create a simple function to do it. When you click the button, this function runs and handles everything.
Simply you only deal with the easy part only clicking the button) and the complicated coding is hidden. This is what abstraction means, making things easier to use by hiding the irrelevant data.
In this blog, we’ll explain abstraction in Python with examples so you can easily understand the concept.
Before learning abstraction in Python, you should have a basic idea about the concepts such as –
- Object-oriented programming (OOP)
- Inheritance
- Polymorphism
These concepts are the building blocks for abstraction. Knowing them helps you understand how abstraction simplifies and organizes code by hiding complex information and focusing on what’s important.
Also, having understanding of –
- Basic Python syntax
- Functions
- Modules
- Decorators
All these basic concepts will help you understand and use abstraction method in Python programming more effectively.
Enroll now and take the first step towards a successful career. Click here to join our Python Programming course today!
Abstract Classes and Methods in Python
- Abstract classes are special classes that have one or more methods without any code inside them and Abstract classes cannot be used to create objects directly.
- An abstract method is a method that is declared in the abstract class but does not have any implementation (no code inside it).
- Subclasses (the classes that inherit from the abstract class) must provide the implementation for these abstract methods.
Example – We’ll create an abstract class called “Appliance” with an abstract method “turn_on.”
Step 1: Import the abc Module
Step 2: Define the Abstract Class
Step 3: Define Subclasses
Create specific appliances like “WashingMachine” and “Refrigerator” that inherit from Appliance and implement the “turn_on” method.
Step 4: Use the Subclasses
Now, you can create objects of “WashingMachine” and “Refrigerator” and call their “turn_on” and “turn_off” methods.
Output –
The washing machine is now running.
The appliance is now off.
The refrigerator is now cooling.
The appliance is now off.
Also Read – The 2024 Python RoadMap – For Beginners
Access Modifiers in Python’s Abstract Classes
In Python, access modifiers in abstract classes are controlled by how methods and attributes are named, not by specific keywords. Here’s how access is usually handled in abstract classes:
1) Public Access
By default, all methods and attributes in Python are public, accessible from anywhere. They are not explicitly marked and can be freely accessed outside the class and its subclasses.
For Example –
2) Protected Access
Conventionally indicated by a single underscore (_). Should be accessed by subclasses only.
For Example –
3) Private Access
Indicated by a double underscore (__). Name mangling makes it somewhat inaccessible outside the class.
For Example –
Also Read – Access Modifiers In Python
What is the Abstract data type in Python?
In Python, an Abstract Data Type (ADT) refers to a conceptual model that defines what operations you can perform on a type of data without worrying about how those operations work.
Python doesn’t have a built-in abstract data type construct like some other languages, but ADTs can be implemented using classes & interfaces.
For example – You can define an abstract base class with abstract methods that represent the operations of the ADT, and then provide concrete implementations in subclasses.
Examples of Abstract data types are –
- Lists
- Queues
- Stacks
- Trees
- Graphs
Also Read – What is Python Programming?
Difference between Abstract class and Interface in Python
Feature | Abstract Class | Interface |
Definition | A class that cannot be instantiated on its own and may contain abstract methods. | Defines a contract for methods that a class must implement. |
Instantiation | Cannot create instances directly; used as a base class. | Cannot include implementation; only method signatures. |
Method Implementation | Can have both abstract (unimplemented) and concrete (implemented) methods. | Only method signatures without implementation details. |
Inheritance | Subclasses must implement all abstract methods or be abstract themselves. | Classes must implement all interface methods they inherit. |
Multiple Inheritance | Supports single inheritance and can have data members. | Supports multiple inheritance and only includes method signatures. |
Purpose | Provides a partial implementation for subclasses to extend. | Specifies a set of methods that a class must implement. |
Also Read – Python Internship Guide For Freshers In 2024
Key Points to Remember –
- Abstraction hides unnecessary details and shows only necessary information.
- Abstract classes cannot be directly instantiated; they are meant to be inherited.
- Abstract methods in these classes define what subclasses must implement.
- Python uses the “abc” module for defining abstract base classes and methods.
- Interfaces are defined with abstract base classes containing method signatures only.
If you want to learn Python from scratch?
Checkout our Python course in Pune
OR call us for career counseling on 8600998107/7028710777