...

/

Solution: Create an Abstract Class and Derive Classes from It

Solution: Create an Abstract Class and Derive Classes from It

Learn how to create the abstract class Vehicle, derive two classes from it, and then call the methods using objects of derived classes.

We'll cover the following...

The solution to the problem of creating the abstract class Vehicle, deriving two classes from it, and then calling the methods using objects of derived classes is given below.

Solution

Press + to interact
from abc import ABC, abstractmethod
class Vehicle(ABC) :
@abstractmethod
def speed(self) :
pass
def maintenance(self) :
pass
def value(self) :
pass
class FourWheeler(Vehicle) :
def speed(self) :
print('In FourWheeler.speed')
def maintenance(self) :
print('In FourWheeler.maintenance')
def value(self) :
print('In FourWheeler.value')
class TwoWheeler(Vehicle) :
def speed(self) :
print('In TwoWheeler.speed')
def maintenance(self) :
print('In TwoWheeler.maintenance')
def value(self) :
print('In TwoWheeler.value')
# v = Vehicle( ) # will result in error, as Vehicle is abstract class
fw = FourWheeler( )
fw.speed( )
fw.maintenance( )
fw.value( )
tw = TwoWheeler( )
tw.speed( )
tw.maintenance( )
tw.value( )

Explanation

...