Inheritance is one of the most important of OOP’s concepts. In this tutorial, we will discuss how can we implement inheritance property using class and objects.
What is Inheritance?
As we know that inheritance is one of the features of OOP’s and it used too often. According to the inheritance concept we can use the properties of one class in another class, and these properties could be its methods and attributes.
In Inheritance, we require at least two classes one would be the base class and the second the derived class. The base class will be a normal class but the derived class will have some modification in its syntax. We could also consider base class as a parent class and derived class as a child class.
class base: # base class block class derived(base): # derived class block
Here in the above syntax, the derived class take the base class name as an argument to inherit its properties.
class Stu: #base class def get(self): self.roll = int(input("Please enter the roll no.: ")) self.m1 = int(input("Please enter the M1 marks: ")) self.m2 = int(input("Please enter the M2 marks: ")) self.ea = int(input("Please enter the extra activity marks: ")) class Output(Stu): # Derived Class def display(self): self.total = self.m1 + self.m2 + self.ea print("Roll No.",self.roll, "total marks are: ", self.total ) student = Output() student.get() student.display()
Please enter the roll no.: 10 Please enter the M1 marks: 95 Please enter the M2 marks: 94 Please enter the extra activity marks: 98 Roll No. 10 total marks are: 28
Behind the code:
In the above example, we have defined 2 classes Stu and Output, here Stu is the base class whereas Output is the derived class which means the Output class can inherit the properties of Stu class.
In Stu class we have defined all the attributes (roll, m1, m2, and ea) inside the get() method and use those attributes in the Output class display method where we make total.
In the above program we did not create the object of Stu class only Output class and with the help of Output object student we accessed the Stu get() method to take input from the user and use those inputs in display() method.
This inherits property used by Output class in which it accessed the Stu class attributes and get() method prove that in python class can follow the inheritance property.
Method overriding means when two class shear the same method name, it falls under OOP’s polymorphism concept but somewhere it also related to inheritance when a class inherit the property of another class and both classes have same method name.
Let’s understand it with an example:
class animal: def __init__(self,name): self.name= name print(self.name,"Animal") def speak(self): print(" Voice ") class cat(animal): def speak(self): print(self.name,"speak meow") tom = cat("tom") #this will invoke the animal __init__ method because cat does not have __init__ tom.speak() #this will invoke cat speak method
tom Animal tom speak meow
Behind the Code:
In the above code when we create the cat object tom and we pass a name “tom” along with the class cat(“tom”) this will invoke the animal class constructor that is __init__() because the cat class does not have any __init__ method. But when we use the tom object and call the speak method then the cat class speak method get invoked not the animal class speak() method why so because the interpreter first looks all the cat class methods if the cat class did not have the speak method then it would call the animal speak method.