Python Inheritance:
Inheritance means inheriting all the methods and properties in a class from another class.
1. Parent class is the class being inherited from, also called base class.
2. Child class is the class that inherits from another class, also called derived class.
Creating a Parent Class:
Any class can be a parent class, so the syntax is the same as creating any other class.
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def printname(self):
print(self.name, self.age)
def __init__(self, name, age):
self.name = name
self.age = age
def printname(self):
print(self.name, self.age)
x = Person("Billy", 35)
x.printname()
x.printname()
Output:
Billy 35
Note: Now we will determine the above class as a Parent Class.
Creating a Child Class:
To create a class that inherits the functionality from another class, send the parent class as a parameter when creating the child class.
Example:
class Student(Person):
pass
pass
Note: In above example we have created Student class which will behave as a child class for Person class.
Note: In above example we have written pass keyword when we do not want to add any method and properties.
Combining Parent and Child Class:
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def printname(self):
print(self.name, self.age)
class Student(Person):
pass
x = Student("Ben", 15)
x.printname()
def __init__(self, name, age):
self.name = name
self.age = age
def printname(self):
print(self.name, self.age)
class Student(Person):
pass
x = Student("Ben", 15)
x.printname()
Output:
Ben 15
__init__( ) function:
Add
__init__()
function to the child class (instead of the pass
keyword).
Note: The
__init__()
function is called automatically every time the class is being used to create a new object. Also not that in __init__ there is two underscore(__).
When you add the
__init__()
function, the child class will no longer inherit the parent's __init__()
function.
Example:
class Student(Person):
def __init__(self, name, age):
def __init__(self, name, age):
Note: The child's
__init__()
function overrides the inheritance of the parent's __init__()
function.
To keep the inheritance of the parent's
__init__()
function, add a call to the parent's __init__()
function:
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def printname(self):
print(self.name, self.age)
class Student(Person):
def __init__(self, name, age):
Person.__init__(self, name, age)
x = Student("Ben", 15)
x.printname()
def __init__(self, name, age):
self.name = name
self.age = age
def printname(self):
print(self.name, self.age)
class Student(Person):
def __init__(self, name, age):
Person.__init__(self, name, age)
x = Student("Ben", 15)
x.printname()
Output:
Ben 15
Now we have successfully added the __init__() function, and kept the inheritance of the parent class, and we are ready to add functionality in the
Now 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.super( ) keyword:
Python also have a
super()
function that will make the child class inherit all the methods and properties from its parent.
By using the
super()
function, you do not have to use the name of the parent element, it will automatically inherit the methods and properties from its parent.
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def printname(self):
print(self.name, self.name)
class Student(Person):
def __init__(self, name, age):
super().__init__(name, age)
x = Student("Ben", 15)
x.printname()
def __init__(self, name, age):
self.name = name
self.age = age
def printname(self):
print(self.name, self.name)
class Student(Person):
def __init__(self, name, age):
super().__init__(name, age)
x = Student("Ben", 15)
x.printname()
Output:
Ben 15
Adding properties in class:
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def printname(self):
print(self.name, self.name)
class Student(Person):
def __init__(self, name, age):
super().__init__(name, age)
self.yearofbirth = year
x = Student("Ben", 15 , 2000)
print(x.yearofbirth)
def __init__(self, name, age):
self.name = name
self.age = age
def printname(self):
print(self.name, self.name)
class Student(Person):
def __init__(self, name, age):
super().__init__(name, age)
self.yearofbirth = year
x = Student("Ben", 15 , 2000)
print(x.yearofbirth)
Output:
2000
Adding methods in class:
If you add a method in the child class with the same name as a function in the parent class, the inheritance of the parent method will be overridden.
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def printname(self):
print(self.name, self.name)
class Student(Person):
def __init__(self, name, age):
super().__init__(name, age)
self.yearofbirth = year
def hello(self):
print("hello", self.name," of age ", self.name," birth ", self.year)
x = Student("Ben", 15 , 2000)
print(x.yearofbirth)
def __init__(self, name, age):
self.name = name
self.age = age
def printname(self):
print(self.name, self.name)
class Student(Person):
def __init__(self, name, age):
super().__init__(name, age)
self.yearofbirth = year
def hello(self):
print("hello", self.name," of age ", self.name," birth ", self.year)
x = Student("Ben", 15 , 2000)
print(x.yearofbirth)
Output:
hello Ben of age 15 birth 2000
0 Comments