Inheritance is basically one of the most important feature of Object Oriented Programming.
Inheritance means acquiring the properties of a class in another class.
The parent class is called Base class while child class is called Derived class.
By using inheritance concept in our script we can reuse the code.
Inheritance saves a lot of time of programmer as writing code again and again is time consuming.
There can be many types of Inheritance. For example
Inheritance means acquiring the properties of a class in another class.
The parent class is called Base class while child class is called Derived class.
By using inheritance concept in our script we can reuse the code.
Inheritance saves a lot of time of programmer as writing code again and again is time consuming.
There can be many types of Inheritance. For example
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Script: Write an script in which class B inherit all the properties of class A.
class A:
def __init__(self, a):
self.a=a
def showA(self):
print("the value of a is "+str(self.a))
class B(A):
def __init__(self,a,b):
super().__init__(a)
self.b=b
def showB(self):
print("The value of b is "+str(self.b))
obj1=B(2,3)
obj1.showA()
obj1.showB()
Output:
the value of a is 2
The value of b is 3
The following points needs to be remember in the above program.
- Child class constructor does not call parent class constructor implicitly in Python language.
- If we make object of child class and we call a function then first it will find out the function in the child class, if the function matches in the child class, then it will not search that function in the parent class.
- If the function is not found in the child class it will search that in the parent class.
Be First to Post Comment !
Post a Comment