8/13/2023 0 Comments Python oop inheritance example![]() It means that DerivedClass2 inherits all the attributes and methods of both DerivedClass1 and SuperClass. In the above example, DerivedClass2 is derived from DerivedClass1, which is derived from SuperClass. # define class that derive from DerivedClass1ĭ2.super_method() # Output: "Super Class method called"ĭ2.derived1_method() # Output: "Derived class 1 method called"ĭ2.derived2_method() # Output: "Derived class 2 method called" # define class that derive from SuperClass Multilevel Inheritance in PythonĮxample: Python Multilevel Inheritance class SuperClass: Here, the DerivedClass1 class is derived from the SuperClass class, and the DerivedClass2 class is derived from the DerivedClass1 class. Here's the syntax of the multilevel inheritance, class SuperClass: This form of inheritance is known as multilevel inheritance. In Python, not only can we derive a class from the superclass but you can also derive a class from the derived class. Here, we are using b1 (object of Bat) to access mammal_info() and winged_animal_info() methods of the Mammal and the WingedAnimal class respectively. In the above example, the Bat class is derived from two super classes: Mammal and WingedAnimal. ![]() Here, the MultiDerived class is derived from SuperClass1 and SuperClass2 classes.Įxample: Python Multiple Inheritance class Mammal: # features of SuperClass1 + SuperClass2 + MultiDerived class Python Multiple Inheritance Syntax class SuperClass1:Ĭlass MultiDerived(SuperClass1, SuperClass2): It makes sense because bat is a mammal as well as a winged animal. This is called multiple inheritance.įor example, A class Bat is derived from superclasses Mammal and WingedAnimal. A class can be derived from more than one superclass in Python.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |