Google News
logo
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.
Imheritance
Syntax (Base Class) :
class BaseClass:
	statement-1
	statement-2
	.....
	.....
	statement-n
Syntax (Derived Class) :
class DerivedClass(BaseClass):
	statement-1
	statement-2
	.....
	.....
	statement-n

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
>>>