educative.io

Need to know why Abstract classes are declaring

Need to know why Vehicle, Parking Spot, Account class is abstract. Is there any specific reason ?

Hello @vishal_garg,
The decision to make classes abstract in the context of the parking lot system has likely been made to enforce certain constraints or design choices specific to the problem domain. Here are some potential reasons for making the Vehicle, ParkingSpot, and Account classes abstract:

  1. Polymorphism and Inheritance: Making these classes abstract allows them to serve as base classes for more specific types of vehicles, parking spots, and accounts. By defining them as abstract, the design encourages the creation of subclasses with specialized implementations. This promotes code reuse and follows the principles of polymorphism and inheritance in object-oriented programming.
  2. Shared Functionality: In many parking lot systems, different types of vehicles may share common behavior, such as assigning a ticket or calculating parking fees. By defining these common functionalities in the abstract base class (Vehicle), the specific vehicle types (e.g., Car, Truck, etc.) can inherit and override these methods as needed, reducing code duplication.
  3. Enforcement of Implementation: Making a class abstract means that it cannot be instantiated directly, but it can only be used as a blueprint for creating objects through its concrete subclasses. This can enforce the implementation of certain methods across all subclasses. For example, an abstract Vehicle class might require its subclasses to implement the assignTicket() method.
  4. Interface for Common Operations: Abstract classes can act as interfaces or contracts, defining a set of methods that all concrete subclasses must implement. In this context, the abstract classes may represent a set of operations that need to be supported by different vehicles, parking spots, and accounts.
  5. Changing Requirements: In a real-world parking lot system, the requirements might change or new vehicle types or account types might be added. By having abstract classes, the system can easily accommodate these changes by creating new subclasses without modifying existing code.

It’s important to note that the decision to make a class abstract or not depends on the specific design goals and requirements of the application. Abstract classes are a powerful tool in object-oriented programming, and their use can lead to more flexible and maintainable code when employed appropriately.

I hope this helps. Happy Learning :slight_smile: