Inheritance in python


Inheritance is a Python process that allows one class to take on the methods and attributes of another. This feature allows users to create more complicated classes that inherit methods or variables from their parent or base classes and makes programming more efficient.

This is the syntax for defining a class that inherits all variables and function from a parent class:

class ChildClass(ParentClass):

To illustrate, you can create a new class, Resigned, that will inherit from the Employees class and take an additional variable, status:

class Employees(object):

def __init__(self, name, rate, hours): = name

self.rate = rate

self.hours = hours

staff = Employees(“Wayne”, 20, 8)

supervisor = Employees(“Dwight”, 35, 8)

manager = Employees(“Melinda”, 100, 8)

print(, staff.rate, staff.hours)

print(, supervisor.rate, supervisor.hours)

print(, manager.rate, manager.hours)

class Resigned(Employees):

def __init__ (self, name, rate, hours, status): = name

self.rate = rate

self.hours = hours

self.status = status

exemp_1 = Resigned(“Dorothy”, 32, 8, “retired”)

exemp_2 = Resigned(“Malcolm”, 48, 8, “resigned”)

print(, exemp_1.rate, exemp_1.hours, exemp_1.status)

print(, exemp_2.rate, exemp_2.hours, exemp_2.status)