- Inheritance is a mechanism in which one class acquires the properties and attributes of another class.
- Using inheritance we can reuse the fields and methods of another class.
In inheritance, there are two terms
1. Base class(Parent Class)
2. subclass(Child class)
- Also called as parent class.
- Parent or base classes create a pattern out of which child or subclasses can be based on.
- Parent classes allow us to create child classes through inheritance without having to write the same code over again each time
- Also called as child classes
- Child or subclasses are classes that will inherit from the parent class.
- That means that each child class will be able to make use of the methods and variables of the parent class.
For inheriting parent class in subclass the parent class name is passed to the child class
Syntax:
#simple parent. Class
class <ParentClassName>:
#attributes
#methods
#inheriting sub class from parent class.
class <ChildClassName>(ParentClassName,ClassName2):
#attributes
#initialize init methods
def __init__(self,variables from parent class and subclass also):
super._init_(attributes from base class)
classname._init_(attributes from base class)
You can derive parent class attributes in child class there are two different ways
1. Using the super keyword
2. Using Class Name
Syntax:
super()._init_(Variable from parent claas)
Example:
#Parent Class
class A:
def _init_(self,vara):
self.vara=vara
#Child Class Inherits Class B
class B(A):
def __init__(self,vara,varb):
#Initilize Attributebuts of parent Class using super
super()._init_(vara)
self.varb=varb
def Display(self):
#print value from class A and Class B
print(self.vara,self.varb)
#Create an object of the child class
b1=B(2,4)
#call method
print("vara and varb Value")
b1.Display()
Output:
vara and varb Value
2 4
Syntax:
Classname._init_(self, Variable from parent class)
Example
#Parent Class
class A:
def _init_(self,vara):
self.vara=vara
#Child Class Inherits Class B
class B(A):
def __init__(self,vara,varb):
#Initilize Attributebuts of parent Class using classname
A._init_(self,vara)
self.varb=varb
def Display(self):
#print value from class A and Class B
print(self.vara,self.varb)
#Create an object of the child class
b1=B(2,4)
#Call Methods
print("vara,varb")
b1.Display()
Output:
vara,varb
2 4