Inheritance (HELP MY DOUBT)


#1



Doubts:
1) Does a derived class inherits the __init__ method of base class
2) In this code why there are not arguments of Equilateral base classes's  __init__ method i.e
why is it def __init__(self) instead of __init__(self, angle1, angle2, angle3)

class Triangle(object):
    number_of_sides = 3
    def __init__(self, angle1, angle2, angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3

    def check_angles(self):
        if self.angle1+self.angle2+self.angle3 == 180:
            return True
        else: 
            return False

my_triangle = Triangle(90, 30, 60)
print my_triangle.check_angles()
print my_triangle.number_of_sides

class Equilateral(Triangle):
    angle = 60
    def __init__(self):
        self.angle1 = self.angle
        self.angle2 = self.angle
        self.angle3 = self.angle


#2

Hi, @manikantaraju ,

Yes, a child class does inherit the __init__ method of its parent class. But we overrode it with a new __init__ method in the Equilateral class. So, when an instance of Equilateral is created, the new __init__ method executes instead of the parent class's __init__ method.

The reason why we overrode the original __init__ method is that in an equilateral triangle, all the internal angles are equal to 60 degrees. So, it does not make sense to have three instance variables to represent angles for an Equilateral object. In fact, we do not need any parameters for angles the __init__ method of the new class, because we already know that all angles will be 60 degrees, and can just set them all to that value.


#3

One more doubt if u could help me out with :sweat_smile:

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
        
    def display_car(self):
        return "This is a %s %s with %d MPG." %(self.color, self.model, self.mpg)
    
    def drive_car(self):
        self.condition = "used"

class ElectricCar(Car):
    def __init__(self, model, color, mpg, battery_type):
        Car.__init__(self, model, color, mpg)
        self.battery_type = battery_type


my_car = ElectricCar("Maserati", "Red", 88, "molten salt")

Why do i have to redefine init method in ElectricCar with all the varialbes

Can't i code ElectricCar like this as it already inherits the Car class

class ElectricCar(Car):

    def __init__(self, battery_type):
        self.battery_type = battery_type

    my_car = ElectricCar("Maserati", "Red", 88, "molten salt")

i understood while i am writing the reply that we are completely overriding the init method, so i should obviously have all the arguments color , mpg etc and call the parent class init method at the same time.

Let me know if i am right.


#4

Hi @manikantaraju ,

Yes, since we have created a new __init__ method for the ElectricCar class, we need to write it in a manner that sets values for any instance variables that we need. So we have parameters in the new __init__ method for communicating each of those values. In the new __init__ method, we call the original one because all of its statements are still useful to us in setting up an instance of the new ElectricCar class. When we create a child class, we do not always have to call the original __init__ method. Whether we do so depends upon whether that method is still useful to us. Quite often it is useful, if our classes are well-designed.