Monday, September 5, 2022

What is inheritance?

Inheritance is a very useful tool in object-oriented programming. The inheritance allows us to define a class that inherits all the methods and properties of another class. The class that inherits all methods and properties from another class is called subclass or child class or derived class. The class from which methods and properties are inherited is called super class or parent class or base class. Any class can be parent class, however for defining the child class after the class name we have to put parenthesis in which we have to write the name of the parent class. The general form of basic class inheritance can be written in the following form.
class FirstClass:
pass
class SecondClass(FirstClass):
pass
In previous code we have created the parent class named FirstClass and then created the child class named as the SecondClass. However in brackets that were created following the name SecondClass we have placed the name of the FirstClass. This means that the child class will inherit the methods and properties of the parent class. As you can see the FirstClass and SecondClass both have pass keyword in the class body, so nothing will happen. ??
Example 1 Create a class ParentCalculator with four basic mathematical operations and create a child class named ChildCalculator that will inherit the methods and properties of ParentCalculator and create methods for calculating the percentage and the square root of both arguments.
Solution:
class ParentCalculator:
def __init__(self, x, y):
self.x = x
self.y = y
def add(self):
return self.x + self.y
def sub(self):
return self.x - self.y
def mul(self):
return self.x*self.y
def div(self):
return self.x/self.y
class ChildCalculator(ParentCalculator):
def percent(self):
return (self.x/self.y)*100
def sqrt(self):
return [(self.x)**(1/2), (self.y)**(1/2)]
Creating the object of the child class:
test = ChildCalculator(8,67)
print("{} + {} = {}".format(test.x, test.y, test.add()))
print("{} - {} = {}".format(test.x, test.y, test.sub()))
print("{}*{} = {}".format(test.x, test.y, test.mul()))
print("{}/{} = {}".format(test.x, test.y, test.div()))
print("({}/{})*100 = {}%".format(test.x, test.y, test.percent()))
print("Sqrt({}) = {}\nSqrt({}) = {}".format(test.x, test.sqrt()[0], test.y, test.sqrt()[1]))
The output.
8 + 67 = 75
8 - 67 = -59
8*67 = 536
8/67 = 0.11940298507462686
(8/67)*100 = 11.940298507462686%
Sqrt(8) = 2.8284271247461903
Sqrt(67) = 8.18535277187245
What would happen if we created the object of the parent class ParentCalculator and used methods from the child class ChildCalculator.
test2 = ParentCalculator(4,7)
            print("({}/{})*100 = {}%".format(test2.x, test2.y, test2.percent()))
The previous code will generate the AttributeError.
AttributeError: 'ParentCalculator' object has no attribute 'percent'
So what we did is created an object of the class ParentCalculator and then applied on the object test2 the method percent which is a method of child class ChildCalculator.

No comments:

Post a Comment