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

Syntax-

class parent():
body
class child(parent):
body


program-

#create class
class Computer():
def name(self):
print("Computer class")

class Machine(Computer):
def work(self):
print("Machine class")

c=Machine()
c.name()
c.work()


output-

Computer class
Machine class


-In this program, we create a two class named as 'Computer()' and Machine(), where Machine(Computer) shows that Machine class inherits the property of the Computer class.
-Parent class is 'Computer()'
-Child class is 'Machine()'
-Here we create an object of the child class i.e Machine() which are also able to call the member function of the parent class i.e. Computer()
-So by c.name() and c.work() we are able to call both data functions of parent and child 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!