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
ABCandabstractmethodfrom theabcmodule to create an abstract base class. - The
BankAppclass inherits fromABC, making it an abstract class that cannot be instantiated directly. - The
databasemethod is a concrete method that prints a message indicating a connection to a database. - The
securitymethod 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.
This code defines a MobileApp class that inherits from BankApp and includes a method for mobile login.
Explanation
- The
MobileAppclass is a subclass ofBankApp, indicating it inherits properties and methods from theBankAppclass. - The
mobile_loginmethod, 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.
This code snippet demonstrates the instantiation of a MobileApp class and emphasizes the need for abstract methods.
Explanation
- The variable
mobis created as an instance of theMobileAppclass. - 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.
Output
This code defines a MobileApp class that inherits from BankApp and includes methods for mobile login and security features.
Explanation
- The
MobileAppclass is a subclass ofBankApp, indicating it inherits properties and methods from theBankAppclass. - The
mobile_loginmethod prints a message indicating that the user is logging into the mobile application. - The
securitymethod 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.
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
mobserves 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
mobreference
This code snippet invokes the security method from the mob object to perform security-related operations.
Explanation
- Calls the
security()method on themobobject, 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 themobclass definition. - This code assumes that the
mobobject is already instantiated and properly configured before this method call.
Output
This code snippet initializes the database connection for the 'mob' object.
Explanation
- Calls the
database()method on themobobject, 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.
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.
This code defines a vehicle class hierarchy with fare calculation for a bus.
Explanation
- The
Vehicleclass initializes withtypeandcapacityattributes and has a methodfarethat calculates the fare based on capacity. - The
Busclass inherits fromVehicleand overrides thefaremethod to include an additional 10% surcharge on the base fare. - An instance of
Busis created with a type of 'school bus' and a capacity of 50. - The
faremethod of theBusinstance is called and prints the total fare, which includes the 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.
This code defines a structure for representing points and their reflections in a 2D space.
Explanation
- The
Pointclass initializes a point with x and y coordinates and has a method to display its coordinates. - The
Locationclass contains twoPointinstances, representing a source and a destination, initialized with given coordinates. - The
showmethod in theLocationclass prints the coordinates of both the source and destination points. - The
reflectionmethod 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
Locationis created with specified coordinates, and both theshowandreflectionmethods are called to demonstrate functionality.
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.
Implementing an abstract base class for geometric shapes in Python
Explanation
- Defines an abstract base class
Polygonwith two abstract methods:get_dataandarea, enforcing a structure for derived classes. - Implements the
Rectangleclass that inherits fromPolygon, providing specific implementations forget_dataandareamethods to calculate the area of a rectangle. - Implements the
Triangleclass similarly, allowing for the calculation of the area of a triangle using its base and height. - Creates instances of
RectangleandTriangle, sets their dimensions usingget_data, and calculates their areas, demonstrating polymorphism in action.
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.
Implementing a billing system with cash and cheque payment options in Python
Explanation
- Defines a
Billclass that calculates the total price of items and displays them. - The
CashPaymentclass inherits fromBill, adding functionality to show cash payment details, including denominations and their values. - The
ChequePaymentclass also inherits fromBill, 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.
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
CashPaymentclass is created using the items, prices, denominations, and values. - The
show_cash_paymentmethod is called on thecashobject to display the payment details.
Output
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
ChequePaymentobject. - If the cash option is selected, predefined denominations and their quantities are used to create a
CashPaymentobject. - Each payment method has a method to display the payment details to the user.
Output
Next in this series: Python File Handling: Read, Write, Serialize & Deserialize →

