Hierarchal inheritance in python by R4R Team

-Inheritance allows us to define a class that inherits all the methods and properties from another class.

Parent class-
it is the class being inherited from, also called base class.

Child class-
Child class is the class that inherits from another class, also called derived class.

Heirarchical Inheritance-
- when more than one child class are inherits the property of the same parent class then this type of inheritance are said to be Heirarchical Inheritance.

Syntax-

class A():
body
class B(A):
body
class C(A):
body
class D(A):
body

-Here class A is a parent class, and class B, class C, class D are child class which inherits the property of the class A.


program-

#create class
class A():
def func1(self):
print("Function of A class")

class B(A):
def func2(self):
print("Function of B class")

class C(A):
def func3(self):
print("Function of C class")

class D(A):
def func4(self):
print("Function of D class")

#create objects
b=B()
b.func1()
b.func2()

c=C()
c.func1()
c.func3()

d=D()
d.func1()
d.func4()


output-

Function of A class
Function of B class
Function of A class
Function of C class
Function of A class
Function of D class


-In this program, we create a 4 class , 3 out of them are inherite the property of remaining one class.
-So we create a object of the child class and call the methods of the parent class




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!