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

Python Inheritance

Inheritance allows classes to inherit attributes and behaviors from other classes. The class inheriting attributes is called the child class, while the class being inherited from is called the parent class. To create a child class, the parent class is passed as a parameter when defining the child class. The child class inherits all of the parent class's methods but can also define its own methods and properties. The __init__() method overrides inheritance but using super() preserves inheritance from the parent class.

Uploaded by

ahmed salem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Python Inheritance

Inheritance allows classes to inherit attributes and behaviors from other classes. The class inheriting attributes is called the child class, while the class being inherited from is called the parent class. To create a child class, the parent class is passed as a parameter when defining the child class. The child class inherits all of the parent class's methods but can also define its own methods and properties. The __init__() method overrides inheritance but using super() preserves inheritance from the parent class.

Uploaded by

ahmed salem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

 Tutorials  Exercises  Get Certified  Services  Bootcamps Spaces Sign Up Log in

Dark mode
Dark code
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP BOOTSTRAP HOW TO W3.CSS C C++ C# REACT R JQUERY DJANGO   
Python Get Started
Python Syntax ADVERTISEMENT
Python Comments
Python Variables
Python Data Types
Python Numbers
Python Casting
Python Strings
Python Inheritance
Python Booleans
❮ Previous Next ❯
Python Operators
Python Lists
Python Tuples
Python Sets Python Inheritance
Python Dictionaries
Python If...Else Inheritance allows us to define a class that inherits all the methods and properties from another class.
Python While Loops
Parent class is the class being inherited from, also called base class.
Python For Loops
Python Functions Child class is the class that inherits from another class, also called derived class.
Python Lambda
Python Arrays
Python Classes/Objects
Create a Parent Class
Python Inheritance
Python Iterators Any class can be a parent class, so the syntax is the same as creating any other class:
Python Polymorphism
Python Scope
Python Modules Example Get your own Python Server
Python Dates
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)

#Use the Person class to create an object, and then execute the printname method:

x = Person("John", "Doe")
x.printname()
COLOR PICKER

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()
ADVERTISEMENT

Try it Yourself »

ADVERTISEMENT

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:

Example

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:

class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
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):
print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)

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.

Test Yourself With Exercises

Exercise:
What is the correct syntax to create a class named Student that will inherit properties and methods from a class named
Person?

class :

Submit Answer »

Start the Exercise

❮ Previous Log in to track progress Next ❯

ADVERTISEMENT

ADVERTISEMENT

Spaces Upgrade Newsletter Get Certified Report Error

Top Tutorials Top References Top Examples Get Certified


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

FORUM | ABOUT

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-2023 by Refsnes Data. All Rights Reserved.


W3Schools is Powered by W3.CSS.

You might also like