Write a python program to Implement Abstraction using Abstract class
This Python program demonstrates the use of abstract classes and method overriding in object-oriented programming. It defines an abstract base class Vehicle and two concrete subclasses Bike and Car. The Vehicle class contains two abstract methods, accelerate and park, which are overridden by the child classes to provide specific implementations for different types of vehicles. Here's an explanation of the program:
- class Vehicle:: This is the abstract base class Vehicle. It contains two abstract methods, accelerate and park, which are defined but have no implementation (indicated by the pass statement). Abstract methods are methods that are declared in the base class but do not have a specific implementation, and they must be overridden by the child classes.
- class Bike(Vehicle):: This is the Bike class, which inherits from the Vehicle class. It overrides the accelerate and park methods to provide specific behavior for a bike.
- class Car(Vehicle):: This is the Car class, which also inherits from the Vehicle class. It overrides the accelerate and park methods to provide specific behavior for a car.
- def accelerate(self, name):: In the Bike and Car classes, the accelerate method is implemented to accept a name parameter and print a message specific to the type of vehicle, indicating that it is accelerating.
- def park(self, name):: In the Bike and Car classes, the park method is implemented to accept a name parameter and print a message specific to the type of vehicle, indicating that it is parking.
- c = Car(): An instance of the Car class is created and assigned to the variable c.
- c.accelerate("Car"): The accelerate method is called on the c object, passing "Car" as the name parameter. It prints a message indicating that the car is accelerating.
- c.park("Car"): The park method is called on the c object, passing "Car" as the name parameter. It prints a message indicating that the car is parking.
- b = Bike(): An instance of the Bike class is created and assigned to the variable b.
- b.accelerate("Bike"): The accelerate method is called on the b object, passing "Bike" as the name parameter. It prints a message indicating that the bike is accelerating.
- b.park("Bike"): The park method is called on the b object, passing "Bike" as the name parameter. It prints a message indicating that the bike is parking.
In this program, the use of an abstract base class (Vehicle) ensures that all subclasses (Bike and Car) provide specific implementations for the accelerate and park methods. This allows you to create different instances of vehicles (bike and car) and call their specific methods to demonstrate the behavior of each type of vehicle.
Source Code
#Abstract Class
class Vehicle:
def acclerate(self,name):
pass
def park(self,name):
pass
class Bike(Vehicle):
def acclerate(self, name):
print(name,"is accelrating @ 60kmph")
def park(self, name):
print(name,"is parked at two wheeler parking")
class Car(Vehicle):
def acclerate(self, name):
print(name,"is accelrating @ 90kmph")
def park(self, name):
print(name,"is parked at four wheeler parking")
c = Car()
c.acclerate("Car")
c.park("Car")
b=Bike()
b.acclerate("Bike")
b.park("Bike")
Output
Car is accelrating @ 90kmph
Car is parked at four wheeler parking
Bike is accelrating @ 60kmph
Bike is parked at two wheeler parking