0

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())
2
  • 3
    It should be class Smartphones(Normalphones): ... Commented Jun 30, 2020 at 7:00
  • 1
    How exactly are you expecting Python to know that Smartphones is supposed to be a derived class of Normalphones? Commented Jun 30, 2020 at 7:01

1 Answer 1

1

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())
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.