constructor in multi-level inheritance by R4R Team

Inheritance-
-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.

Multi-level Inheritance-

Syntax-
class A():
body
class B(A):
body
class C(B):
body

Constructor-
-constructor is a special type of function of the class which is automatically call when the instance of the class is created.

Syntax-

def __init__(self,arg):
body


program-

#create parent class
class A():
def __init__(self):
print("Constructor of class A")

#class B inherits the class A
class B(A):
def __init__(self):
print("Constructor of class B")

#class C inherits the class B
class C(B):
def __init__(self):
print("Constructor of class C")

#create objects
c=C()

b=B()

a=A()


output-

Constructor of class C
Constructor of class B
Constructor of class A


-In this program, we create a constructor in multi-level inheritance, so when we create a object of childs, constructor of only that class are called.




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!