Python Inheritance

Python Inheritance
Inheritance is a property or characterstic of any object oriented programming language that will allow a class (called as child class) to inherit all the properties and methods from another class (called as parent class)

Parent class is the class that is being inherited and it is also called as the base class. .

Child class is the class that inherits the properties or methds from the base class and it is also called as the derived class.

Create a Parent Class
Any class can be made as the parent class, so there is no specific syntax for creating the base class that means that the syntax is same just like any other class.
For e.g. if we want to create a class named Person, with firstname and lastname properties, and a printname method, then we write the following code for that:

class Person:

     def __init__(self, fname, lname):

         self.firstname = fname

         self.lastname = lname

def printname(self):

     print(self.firstname, self.lastname)

#Use the Person class to create an object, and then execute the printname method:

x = Person("John", "Doe")

x.printname()


Create a Child Class
When we create a child class that will inherit the properties of another class then we pass the name of the base class as parameter to the child class.
For e.g. if we want to create a class named Student, that will inherit the properties and methods from the Person class, then we write the following code for that:

class Student(Person):

     pass


Now the class Student has the same properties and methods as the Person class as the class Student has inherited the class Person.
Consider the following example to use the class Student to create an object and then execute the printname method:

x = Student("Mike", "Olsen")

x.printname()


Add the __init__() Function
Till now we have created a child class that has inherited the methods and properties of it's parent class.

Now We want to add the __init__() method to the child class (instead of the pass keyword).
Now to add the __init__() function to the Student class, we write the following code:

class Student(Person):

     def __init__(self, fname, lname):

         #add properties etc


When you add the __init__() function, the child class will no longer inherit the parent's __init__() function.

To keep the inheritance of the parent's __init__() function, we need to add a call to the parent's __init__() function as follows:

class Student(Person):

     def __init__(self, fname, lname):

     Person.__init__(self, fname, lname)


Since we have successfully added the __init__() function, and kept the inheritance of the parent class, and we are ready to add functionality in the __init__() function.

Add Properties
Example: Add a property called graduationyear to the Student class:

class Student(Person):

     def __init__(self, fname, lname):

     Person.__init__(self, fname, lname)

     self.graduationyear = 2019


In the following example, the year 2019 should be a variable, and passed into the Student class when creating student objects. To do this, add another parameter in the __init__() function as follows:
Example: Add a parameter say year, and pass the correct year when creating the objects:

class Student(Person):

     def __init__(self, fname, lname, year):

     Person.__init__(self, fname, lname)

     self.graduationyear = year

x = Student("Mike", "Olsen", 2019)


Add Methods
Let us understand this with the help of an example:
Example: Add a method called welcome to the class Student:

class Student(Person):

     def __init__(self, fname, lname, year):

     Person.__init__(self, fname, lname)

     self.graduationyear = year

def welcome(self):

     print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)


When thechild class and the parent class has the methods with the same name then at that time the method present in the parent class will be overridden in inheritance.