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.
Example 1
Explanation
- Defines an abstract base class called BankApp that cannot be instantiated directly
- Imports ABC class and abstractmethod decorator from the abc module to create abstract classes
- Contains one concrete method called database() that prints a connection message when called
- Includes one abstract method named security() that has no implementation (pass statement) and must be overridden by subclasses
- Any class inheriting from BankApp must implement the security() method, otherwise it will raise a TypeError when trying to instantiate it
Example 2
Explanation
- This code defines a new class called
MobileAppthat inherits from an existingBankAppclass - The
mobile_loginmethod is created within this class to handle mobile application login functionality - When called, this method simply prints the text 'login into mobile' to the console
- The class uses inheritance to extend the functionality of the parent
BankAppclass - This method would be called on instances of
MobileAppto perform mobile-specific login operations
Example 3
Explanation
- Creates an instance of a MobileApp class called 'mob'
- The comment suggests that an abstract method must be implemented or used somewhere in the code
- Abstract methods are typically defined in parent classes and must be overridden by child classes
- This indicates the MobileApp class likely has abstract methods that need to be implemented
- The code sets up a foundation for inheritance or interface implementation but doesn't execute any concrete functionality yet
Output
Example 4
Explanation
- This code defines a new class called
MobileAppthat inherits from an existingBankAppclass, meaning it copies all methods and attributes fromBankAppand can add its own - The class implements two specific methods:
mobile_login()which prints 'login into mobile' when called, andsecurity()which prints 'mobile security' - These methods are unique to the
MobileAppclass and demonstrate how inheritance allows extending base functionality while adding mobile-specific behavior - No parameters are needed when calling these methods, and they produce simple text output to the console
- The class structure suggests this is part of a banking application where mobile apps have their own login process and security measures separate from other app types
Example 5
Explanation
- Creates a new instance of a MobileApp class called 'mob'
- Uses the MobileApp constructor to initialize the object
- No parameters are passed to the constructor
- This creates a mobile application object that can be used to call other methods
- The variable 'mob' now references this new mobile app instance in memory
Example 6
Explanation
- Calls the security method on the mob object, likely performing security-related operations
- Commonly used in game development or simulation environments to check permissions or validate actions
- May return boolean value indicating success/failure of security checks
- Could trigger authentication processes or access control validations
- Side effects might include logging security events or modifying mob's state based on security status
Output
Example 7
Explanation
- Calls the database() method on a mob object, likely initializing or connecting to a database
- This appears to be part of a larger application framework or library that manages database connections
- The method probably sets up database configuration, credentials, or connection parameters
- No return value is shown, suggesting it may modify the object's internal state or establish a connection
- Common in web frameworks or data management libraries where database setup happens during initialization
Output
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.
Example 8
Explanation
- Defines a Vehicle class with type and capacity attributes, and a fare method that calculates 100 times the capacity
- Creates a Bus subclass that inherits from Vehicle and overrides the fare method to add 10% to the parent's fare calculation
- Uses super() to call the parent class's fare method and then applies a 10% surcharge for bus fare calculation
- Instantiates a Bus object with type 'school bus' and capacity 50
- Prints the calculated bus fare which is 5500.0 (5000 base fare plus 10% surcharge)
Output
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.
Example 9
Explanation
- Creates two classes
PointandLocationto represent coordinate points and locations with source and destination Pointclass stores x,y coordinates and has method to display them as string formatLocationclass initializes with four coordinates creating source and destination points, then shows their positions- Calls
show()method to print current source and destination coordinates - Executes
reflection()method which flips the y-coordinate of destination point and prints new reflected position
Output
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.
Example 10
Explanation
- Defines an abstract base class Polygon with two abstract methods get_data and area that must be implemented by subclasses
- Creates two concrete classes Rectangle and Triangle that inherit from Polygon and provide their own implementations of the abstract methods
- Rectangle class stores length and breadth, calculates area as length × breadth
- Triangle class stores base and height, calculates area as 0.5 × base × height
- Creates instances of both classes, sets their dimensions, prints rectangle area (20), calculates triangle area (10.0) but doesn't print it
Output
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.
Example 11
Explanation
- Creates a Bill class that stores items, prices, and calculates total cost by summing all prices
- Implements display method to show item names and prices in a formatted table with total amount
- Defines CashPayment subclass that inherits from Bill and adds cash denomination details for payment breakdown
- Creates ChequePayment subclass that inherits from Bill and includes cheque number and bank name for payment details
- Both subclasses use super() to call parent Bill methods and extend functionality with their specific payment information
Example 12
Explanation
- Creates lists for items, their prices, available denominations, and their values for cash payment calculation
- Initializes a CashPayment object with the item data and denomination information
- Calls show_cash_payment() method which likely displays or calculates the total amount due and change breakdown
- Uses a CashPayment class that processes cash transactions based on item prices and available denominations
- The output would show the cash payment details including total cost and how denominations are used for payment
Output
Example 13
Explanation
- Takes user input to choose payment method (cheque or cash) and processes the corresponding payment type
- Creates a ChequePayment object with items, prices, cheque number, and bank name when user selects cheque option
- Creates a CashPayment object with items, prices, denomination values, and quantity values when user selects cash option
- Calls show_cheque_payment() method if cheque option is chosen, otherwise calls show_cash_payment() method
- Displays payment details including item names, prices, and payment information based on user's selected payment method
Output

