Python Inheritance
Inheritance is used to specify that one class will get most or all of its features from its parent class. It is a feature of Object Oriented Programming. The new class is called derived (or child) class and the one from which it inherits is called the base (or parent) class.
The child class or derived class inherits the features from the parent class, adding new features to it. It facilitates re-usability of code.
Syntax (Base Class) :
class BaseClass:
Syntax (Derived Class) :
class DerivedClass(BaseClass):

Python Inheritance Example

The name BaseClassName must be defined in a scope containing the derived class definition. With all this said, we can implement our Person and Employee class is as following example :

>>> class Person:

    def __init__(self, first, last):
        self.firstname = first
        self.lastname = last

    def Name(self):
        return self.firstname + " " + self.lastname

>>> class Employee(Person):

    def __init__(self, first, last, staffnum):
        Person.__init__(self,first, last)
        self.staffnumber = staffnum

    def GetEmployee(self):
        return self.Name() + ", " +  self.staffnumber

>>> x = Person("Ramana", "Chanti")
>>> y = Employee("Suresh", "Raja", "1017")
>>> print(x.Name())
Ramana Chanti
>>> print(y.GetEmployee())
Suresh Raja, 1017

We see you’re using an ad blocker.

Our website is made possible by displaying ads to our visitors.

Please help us continue to provide you with free. So please disabling your ad blocker.