

Print(issubclass(SalesEmployee, Person)) # True Code language: Python ( python )

It’s also a subclass of the Person class as shown in the following: print(issubclass(SalesEmployee, Employee)) # True The SalesEmployee is the subclass of the Employee class. The following defines the SalesEmployee class that inherits from the Employee class: class SalesEmployee (Employee): pass Code language: Python ( python ) For example: print(issubclass(Employee, Person)) # True Code language: Python ( python ) To check if a class is a subclass of another class, you use the issubclass() function. Then, you’ll call the methods of that object. In practice, you’ll often use the isinstance() function to check whether an object has certain methods.

To check if an object is an instance of a class, you use the isinstance() method. Print(type(employee)) Code language: Python ( python ) The following shows the type of instances of the Person and Employee classes: person = Person( 'Jane')Įmployee = Employee( 'John', 'Python Developer') The relationship between the Employee class and Person class is IS-A relationship. The Employee class derives from, extends, or subclasses the Person class. And the Employee class is a child class, a derived class, or a subclass of the Person class. The Person class is the parent class, the base class, or the super class of the Employee class. Output: Hi, it 's John Code language: Python ( python ) Inheritance terminology Print(eet()) Code language: Python ( python ) Since the Employee inherits attributes and methods of the Person class, you can use the instance of the Employee class as if it were an instance of the Person class.įor example, the following creates a new instance of the Employee class and call the greet() method: employee = Employee( 'John', 'Python Developer') Note that Python also supports multiple inheritances where a class inherits from multiple classes. This is a single inheritance because the Employee inherits from a single class ( Person). Self.job_title = job_title Code language: Python ( python )īy doing this, the Employee class behaves the same as the Person class without redefining the greet() method. The following redefines the Employee class that inherits from the Person class: class Employee (Person): def _init_ (self, name, job_title):

To do it, you use inheritance so that the Employee class inherits from the Person class. To reuse the greet() method from the Person class in the Employee class, you can create a relationship between the Person and Employee classes. It also has the greet() method that is exactly the same as the greet() method of the Person class. The Employee class has two attributes name and job_title. Suppose you have the following Person class: class Person: def _init_ (self, name):ĭef greet (self): return f"Hi, it's " Code language: Python ( python ) Inheritance allows a class to reuse the logic of an existing class.
PYTHON INHERITANCE HOW TO
Summary: in this tutorial, you’ll learn about Python inheritance and how to use the inheritance to reuse code from an existing class.
