Python InheritanceInheritance is an important aspect of the object-oriented paradigm. Inheritance provides code reusability to the program because we can use an existing class to create a new class instead of creating it from scratch. In inheritance, the child class acquires the properties and can access all the data members and functions defined in the parent class. A child class can also provide its specific implementation to the functions of the parent class. In this section of the tutorial, we will discuss inheritance in detail. In python, a derived class can inherit base class by just mentioning the base in the bracket after the derived class name. Consider the following syntax to inherit a base class into the derived class. SyntaxA class can inherit multiple classes by mentioning all of them inside the bracket. Consider the following syntax. SyntaxExample 1Output: dog barking Animal Speaking Python Multi-Level inheritanceMulti-Level inheritance is possible in python like other object-oriented languages. Multi-level inheritance is archived when a derived class inherits another derived class. There is no limit on the number of levels up to which, the multi-level inheritance is archived in python. The syntax of multi-level inheritance is given below. SyntaxExampleOutput: dog barking Animal Speaking Eating bread... Python Multiple inheritancePython provides us the flexibility to inherit multiple base classes in the child class. The syntax to perform multiple inheritance is given below. SyntaxExampleOutput: 30 200 0.5 The issubclass(sub,sup) methodThe issubclass(sub, sup) method is used to check the relationships between the specified classes. It returns true if the first class is the subclass of the second class, and false otherwise. Consider the following example. ExampleOutput: True False The isinstance (obj, class) methodThe isinstance() method is used to check the relationship between the objects and classes. It returns true if the first parameter, i.e., obj is the instance of the second parameter, i.e., class. Consider the following example. ExampleOutput: True Method OverridingWe can provide some specific implementation of the parent class method in our child class. When the parent class method is defined in the child class with some specific implementation, then the concept is called method overriding. We may need to perform method overriding in the scenario where the different definition of a parent class method is needed in the child class. Consider the following example to perform method overriding in python. ExampleOutput: Barking Real Life Example of method overridingOutput: Bank Rate of interest: 10 SBI Rate of interest: 7 ICICI Rate of interest: 8 Data abstraction in pythonAbstraction is an important aspect of object-oriented programming. In python, we can also perform data hiding by adding the double underscore (___) as a prefix to the attribute which is to be hidden. After this, the attribute will not be visible outside of the class through the object. Consider the following example. ExampleOutput: The number of employees 2 AttributeError: 'Employee' object has no attribute '__count' Next TopicAbstraction in Python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India