Sujan Joshi
Sujan Joshi

Reputation: 3

Inheritance in derived class not called

I am new to this stack overflow and this is my first question here. I am learning python and I have a problem with inheritance. I think my code is right but it is not inheriting parent/ base class from derived/ child class. I tried both ways of inheritance. It is printing full name for Normalphones class but not for Smartphones. My code is here. Help me.

class Normalphones:  # base class/ parent class
    def __init__(self, brand, model_name, price):
        self.brand = brand
        self.model_name = model_name
        self._price = max(price, 0)


    def full_name(self):
        return f"{self.brand} {self.model_name}"
    
    
    def make_a_call(self, number):
        return f"Calling {number} ...."


class Smartphones:  # derived class / child class
    def __init__(self, brand, model_name, price, ram, internal_memory, rear_camera):
        Normalphones.__init__(self, brand, model_name, price)  # uncommom way to inherit
        # super().__init__(brand, model_name, price)
        self.ram = ram
        self.internal_memory = internal_memory
        self.rear_camera = rear_camera


noramalphone1 = Normalphones('Nokia', '1150', 1200)
smartphone1 = Smartphones('I-phone', '11 max pro', 90000, '6 Gb', '128 Gb', '48 MP')

print(noramalphone1.full_name())
print(smartphone1.full_name())

Upvotes: 0

Views: 139

Answers (1)

Pramote Kuacharoen
Pramote Kuacharoen

Reputation: 1541

This is how you inherit: class Smartphones(Normalphones):

class Normalphones:  # base class/ parent class

    def __init__(self, brand, model_name, price):
        self.brand = brand
        self.model_name = model_name
        self._price = max(price, 0)

    def full_name(self):
        return f"{self.brand} {self.model_name}"

    def make_a_call(self, number):
        return f"Calling {number} ...."


class Smartphones(Normalphones):  # derived class / child class
    def __init__(self, brand, model_name, price, ram, internal_memory, rear_camera):
        super().__init__(brand, model_name, price)
        self.ram = ram
        self.internal_memory = internal_memory
        self.rear_camera = rear_camera


noramalphone1 = Normalphones('Nokia', '1150', 1200)
smartphone1 = Smartphones('I-phone', '11 max pro',
                          90000, '6 Gb', '128 Gb', '48 MP')

print(noramalphone1.full_name())
print(smartphone1.full_name())

Upvotes: 1

Related Questions