0% found this document useful (0 votes)
2 views

Python Inheritance

The document explains Python inheritance, detailing how a child class can inherit properties and methods from a parent class. It provides examples of creating a parent class, defining a child class, and using the __init__() function to initialize properties. Additionally, it covers the use of the super() function to facilitate inheritance and demonstrates how to add methods and properties to the child class.

Uploaded by

saifali.modi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Inheritance

The document explains Python inheritance, detailing how a child class can inherit properties and methods from a parent class. It provides examples of creating a parent class, defining a child class, and using the __init__() function to initialize properties. Additionally, it covers the use of the super() function to facilitate inheritance and demonstrates how to add methods and properties to the child class.

Uploaded by

saifali.modi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

11/25/24, 2:11 PM Python Inheritance

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

Python Inheritance
❮ Previous Next ❯

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

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

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

Create a Parent Class


Any class can be a parent class, so the syntax is the same as creating any other
class:

Example Get your own Python Server

Create a class named Person , with firstname and lastname properties, and a
printname method:

class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname

def printname(self):
print(self.firstname, self.lastname)

https://www.w3schools.com/python/python_inheritance.asp 1/9
11/25/24, 2:11 PM Python Inheritance

#Use the
Tutorials
Person Exercises
 class  Services
to create 
an object,  execute the
and then Sign Up Log in
printname
method:
HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
x = Person("John", "Doe")
x.printname()

Try it Yourself »

Create a Child Class


To create a class that inherits the functionality from another class, send the parent
class as a parameter when creating the child class:

Example
Create a class named Student , which will inherit the properties and methods from
the Person class:

class Student(Person):
pass

Note: Use the pass keyword when you do not want to add any other properties or
methods to the class.

Now the Student class has the same properties and methods as the Person class.

Example
Use the Student class to create an object, and then execute the printname method:

x = Student("Mike", "Olsen")
x.printname()

https://www.w3schools.com/python/python_inheritance.asp 2/9
11/25/24, 2:11 PM Python Inheritance

Try it Yourself »
Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

Add the __init__() Function


So far we have created a child class that inherits the properties and methods from its
parent.

We want to add the __init__() function to the child class (instead of the pass
keyword).

Note: The __init__() function is called automatically every time the class is being
used to create a new object.

Example
Add the __init__() function to the Student class:

class Student(Person):
def __init__(self, fname, lname):
#add properties etc.

When you add the __init__() function, the child class will no longer inherit the
parent's __init__() function.

Note: The child's __init__() function overrides the inheritance of the parent's
__init__() function.

To keep the inheritance of the parent's __init__() function, add a call to the
parent's __init__() function:

https://www.w3schools.com/python/python_inheritance.asp 3/9
11/25/24, 2:11 PM Python Inheritance

 Tutorials 
Example
Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)

Try it Yourself »

Now we have successfully added the __init__() function, and kept the inheritance
of the parent class, and we are ready to add functionality in the __init__() function.

Use the super() Function


Python also has a super() function that will make the child class inherit all the
methods and properties from its parent:

Example
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)

Try it Yourself »

By using the super() function, you do not have to use the name of the parent
element, it will automatically inherit the methods and properties from its parent.

Add Properties

Example
Add a property called graduationyear to the Student class:

https://www.w3schools.com/python/python_inheritance.asp 4/9
11/25/24, 2:11 PM Python Inheritance

class Student(Person):
Tutorials  Exercises  Services 
def __init__(self, fname, lname):
 Sign Up Log in

super().__init__(fname, lname)
HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
self.graduationyear = 2019

Try it Yourself »

In the example below, the year 2019 should be a variable, and passed into the
Student class when creating student objects. To do so, add another parameter in the
__init__() function:

Example
Add a year parameter, and pass the correct year when creating objects:

class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year

x = Student("Mike", "Olsen", 2019)

Try it Yourself »

Add Methods

Example
Add a method called welcome to the Student class:

class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year

def welcome(self):

https://www.w3schools.com/python/python_inheritance.asp 5/9
11/25/24, 2:11 PM Python Inheritance

print("Welcome", self.firstname, self.lastname, "to the class of",


 Tutorials  Exercises 
self.graduationyear) Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Try it Yourself »

If you add a method in the child class with the same name as a function in the parent
class, the inheritance of the parent method will be overridden.

?
Exercise
What is the correct keyword to use inside an empty class, to avoid getting an
error?

empty

inherit

pass

Submit Answer »

❮ Previous Next ❯

W3schools Pathfinder
Track your progress - it's free! Sign Up Log in

https://www.w3schools.com/python/python_inheritance.asp 6/9
11/25/24, 2:11 PM Python Inheritance

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

COLOR PICKER



 PLUS SPACES GET CERTIFIED

FOR TEACHERS FOR BUSINESS CONTACT US

Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
https://www.w3schools.com/python/python_inheritance.asp 7/9
11/25/24, 2:11 PM Python Inheritance
Java Tutorial

 C++ Tutorial
Tutorials  Exercises 
jQuery Tutorial
Services   Sign Up Log in

HTML
 CSS
TopJAVASCRIPT
References SQL PYTHON JAVA PHP HOW TO W3.CSS C
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    

FORUM ABOUT ACADEMY


W3Schools is optimized for learning and training. Examples might be simplified to
improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our
terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by

https://www.w3schools.com/python/python_inheritance.asp 8/9
11/25/24, 2:11 PM Python Inheritance
W3.CSS.

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

https://www.w3schools.com/python/python_inheritance.asp 9/9

You might also like