Inheritance in Python Example Programs-DecodingDevOps

Inheritance in Python Example Programs

Inheritance is an important and powerful aspect of object-oriented programming. Inheritance forms a new class from an already defined class. The newly derived ones are called derived classes and the class from which we derive are known as the base class. Inheritance is used to reduce the complexity of the code and it can be used for code reusability. The parent classes extend the functionality of the child classes. it represents a real-world relationship between the parent class and the child class. It is transitive in nature. If a child class inherits properties from a parent class, then all other sub-classes of the child class will also inherit the properties of the parent class. Let’s see one of the examples of the inheritance.

class Animal:
    def __init__(self):
        print("Animal created")

    def whoAmI(self):
        print("Animal")

    def eat(self):
        print("Eating")

class Dog(Animal):
    def __init__(self):
        Animal.__init__(self)
        print("Dog created")

    def whoAmI(self):
        print("Dog")

    def bark(self):
        print("Woof!")

OUTPUT:
Animal created
Dog created
Dog
Eating
Woof!

There are three types of inheritance and let’s check each one of them separately:

1.Multiple Inheritance:  This type of inheritance takes place when one child class inherits from one or more parent classes. The constructors of inherited classes are called in the same order in which they are inherited. On calling the method, the compiler cannot determine which class method to be called and even on calling which class method gets the priority.

Class addition:                           # parent class
      def sum(self,a,b):
           return a+b
Class subtraction:                        # parent class
      def diff(self,a,b):
           return a-b
Class derived(addition,subtraction):      # child class
      def divide(self,a,b): 
           return a/b

d = derived()
print(d.addition(10,20))
print(d.subtraction(50,40))
print(d.divide(20,10))

OUTPUT:
30
10
2

2.MULTILEVEL INHERITANCE:  This type of inheritance takes place when one class is derived from another class which is also derived from another class. Multi-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.

class Animal:  
    def speak(self):  
        print("Animal Speaking")  
#The child class Dog inherits the base class Animal  
class Dog(Animal):  
    def bark(self):  
        print("dog barking")  
#The child class puppy inherits another child class Dog  
class puppy(Dog):  
    def eat(self):  
        print("Eating bread...")  
d = puppy()  
d.bark()  
d.speak()  
d.eat()

OUTPUT:
dog barking
Animal Speaking
Eating bread

METHOD OVERRIDING: it  is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes.

class Animal:  
    def speak(self):  
        print("speaking")  
class Dog(Animal):  
    def speak(self):  
        print("Barking")  
d = Dog()  
d.speak

Leave a Reply

Your email address will not be published. Required fields are marked *