1. What is Abstraction in Python?
Definition:
Abstraction in Python is the concept of hiding internal details and showing only the essential features of an
object.
It allows programmers to focus on what an object does instead of how it does it.
2. Why Use Abstraction?
To reduce complexity.
To protect data from being accessed directly.
To create a blueprint that other classes can follow.
Helps in code reusability and maintainability.
3. How to Implement Abstraction in Python?
Python uses the abc module (Abstract Base Classes) to implement abstraction.
Abstract classes cannot be instantiated.
They may contain abstract methods (methods without implementation).
Subclasses must implement all abstract methods.
Steps:
1. Import ABC and abstractmethod from abc.
2. Create a class inheriting from ABC.
3. Define abstract methods using @abstractmethod.
4. Subclasses must override abstract methods.
4. Types of Abstraction
1. Data Abstraction (Hides data representation, shows interface).
2. Process Abstraction (Hides implementation details of a method).
5. Example Programs (With Output)
Example 1: Basic Abstraction
from abc import ABC, abstractmethod
# Abstract class
class Animal(ABC):
@abstractmethod
def make_sound(self):
pass
# Concrete class
class Dog(Animal):
def make_sound(self):
print("Woof!")
class Cat(Animal):
def make_sound(self):
print("Meow!")
# Using abstraction
d = Dog()
d.make_sound()
c = Cat()
c.make_sound()
Output:
Woof!
Meow!
Example 2: Data Abstraction
class Car:
def __init__(self):
self.__speed = 0 # private variable
def accelerate(self):
self.__speed += 10
def get_speed(self):
return self.__speed
c = Car()
c.accelerate()
print("Speed:", c.get_speed())
Output:
Speed: 10
Here, speed is hidden from the outside world. Only specific methods can access it.
Example 3: Multiple Abstract Methods
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
class Rectangle(Shape):
def __init__(self, l, w):
self.length = l
self.width = w
def area(self):
return self.length * self.width
def perimeter(self):
return 2 * (self.length + self.width)
r = Rectangle(5, 3)
print("Area:", r.area())
print("Perimeter:", r.perimeter())
Output:
Area: 15
Perimeter: 16
Example 4: Abstraction in Real-time Scenario (ATM)
from abc import ABC, abstractmethod
class ATM(ABC):
@abstractmethod
def withdraw(self, amount):
pass
@abstractmethod
def check_balance(self):
pass
class MyBank(ATM):
def __init__(self):
self.balance = 1000
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
print(f"Withdrawn: {amount}")
else:
print("Insufficient Balance!")
def check_balance(self):
print(f"Balance: {self.balance}")
b = MyBank()
b.check_balance()
b.withdraw(300)
b.check_balance()
Output:
Balance: 1000
Withdrawn: 300
Balance: 700
6. Key Points to Remember
Concept Explanation
ABC Abstract Base Class used for abstraction
@abstractmethod Decorator to declare an abstract method
Abstract Class A class with one or more abstract methods
Concrete Class Subclass that implements all abstract methods
Cannot Instantiate Abstract classes cannot be instantiated directly
7. Summary: Different Ways to Use Abstraction
Way Example
Using abc module Defining abstract base classes
Using Private Members Hiding internal state with __var
Defining Interfaces Only method signatures, implemented later
Real-time Systems ATM, Vehicles, Payment Gateways, etc.
8. Common Interview Q&A
Q: Can an abstract class have concrete methods?
Yes, it can have both abstract and concrete methods.
Q: Can we instantiate an abstract class?
No, only subclasses can be instantiated after implementing all abstract methods.
Q: Can abstract class have constructor?
Yes, abstract classes can have constructors.
TOP 25 MCQs on Python Abstraction
1. What is the purpose of abstraction in Python?
A) Increase code size
B) Reduce performance
C) Hide implementation details and show only functionality
D) Make code unreadable
Answer: C
Explanation: Abstraction hides internal details and exposes only necessary features.
2. Which module is used to implement abstraction in Python?
A) abstract
B) abc
C) def
D) abs
Answer: B
Explanation: Python’s abc module (Abstract Base Classes) provides infrastructure for defining abstract base
classes.
3. Which keyword is used to create an abstract method?
A) @method
B) @abstract
C) @abstractmethod
D) abstract
Answer: C
Explanation: The @abstractmethod decorator marks a method as abstract.
4. Can we create objects from abstract classes?
A) Yes
B) Only with parameters
C) No
D) Only in subclasses
Answer: C
Explanation: Abstract classes cannot be instantiated directly.
5. What happens if a subclass does not implement all abstract methods?
A) It raises a warning
B) It runs as normal
C) It raises an error
D) It will become abstract itself
Answer: D
Explanation: The subclass becomes abstract until all abstract methods are implemented.
6. Which of the following is NOT a feature of abstraction?
A) Hiding data
B) Simplifying complexity
C) Showing all implementation
D) Exposing essential information
Answer: C
Explanation: Abstraction hides unnecessary implementation details.
7. Abstract class can have:
A) Only abstract methods
B) Only concrete methods
C) Both abstract and concrete methods
D) No methods
Answer: C
Explanation: Abstract classes can have both abstract and implemented (concrete) methods.
8. What is the correct way to define an abstract class?
A)
class MyClass:
pass
B)
class MyClass(ABC):
pass
C)
from abc import ABC
class MyClass(ABC):
pass
D) Both B and C
Answer: D
Explanation: Abstract class must inherit from ABC, which comes from abc module.
9. What does abc.ABC stand for?
A) Abstract Base Class
B) Abstract Binary Code
C) Abstract Block Creator
D) Abstract Byte Class
Answer: A
10. Why do we use abstraction in OOP?
A) Increase visibility of all logic
B) Reduce modularity
C) Separate interface from implementation
D) Complicate the structure
Answer: C
11. Which of the following is correct?
A) Abstract class must have only one method
B) Abstract class can be instantiated
C) Abstract class must have at least one abstract method
D) Abstract class should not be inherited
Answer: C
12. Choose the real-time example of abstraction:
A) Television remote
B) Car engine working
C) File system in OS
D) All of the above
Answer: D
Explanation: All hide internal logic and expose functionality.
13. In abstraction, we focus on:
A) How something works
B) What something does
C) Both A and B
D) None
Answer: B
14. What is an interface in abstraction?
A) Blueprint of class methods
B) Main logic class
C) Parent class of Python
D) None
Answer: A
15. What will happen if abstract methods are not implemented in subclass?
A) Nothing
B) Warning
C) Runtime Error
D) TypeError
Answer: D
Explanation: A TypeError will be raised when you try to instantiate a subclass that hasn’t implemented all
abstract methods.
16. What is true about abstract method?
A) Has no implementation in base class
B) Cannot be called
C) Must be private
D) Must be overloaded
Answer: A
17. Can an abstract class have constructor?
A) Yes
B) No
C) Only if abstract
D) Only if inherited
Answer: A
Explanation: Constructors can be used in abstract classes.
18. Abstract class in Python starts from which version?
A) Python 1.x
B) Python 2.6+
C) Python 3.5+
D) Python 2.0
Answer: B
19. What is the output?
from abc import ABC, abstractmethod
class A(ABC):
@abstractmethod
def show(self):
pass
class B(A):
pass
b = B()
A) Compiles
B) Runtime Error
C) Instantiates B
D) Syntax Error
Answer: B
Explanation: B does not implement show, so cannot be instantiated.
20. Can we define properties in abstract class?
A) Yes
B) No
C) Only static
D) Only public
Answer: A
21. Is it possible to override an abstract method?
A) No
B) Yes, only in parent
C) Yes, in child class
D) Not needed
Answer: C
22. Abstract class can have:
A) Only variables
B) Only static methods
C) Abstract methods and other members
D) Loops only
Answer: C
23. Which method hides internal details of a class?
A) print()
B) private method
C) abstract method
D) static method
Answer: C
24. Which statement is true about @abstractmethod?
A) It defines a complete method
B) It prevents method overriding
C) It enforces implementation in subclass
D) It defines static methods
Answer: C
25. What is the benefit of abstraction?
A) Hide complex logic
B) Increase error rate
C) Decrease performance
D) Break modularity
Answer: A
Summary Table
Topic Coverage
Module abc
Abstract Class Cannot be instantiated
Abstract Method Must be overridden
Inheritance Required for use
Output Errors if abstract methods not implemented
Uses Hide logic, show interface, real-time systems