user12062967
user12062967

Reputation:

cant override method in a base class with my code

   class Vehicle():

        def __init__(self, brand, model):

            print('Am a vehicle')

        def brand_name(self, brand):
            print('vehicle class')
            self.brand = brand
            return brand


        def Vehicle_model(self, model):
            print('vehicle class')

            self.model = model
            return model


     class Mazda(Vehicle):
            def __init__(self, model, brand):



            Vehicle.__init__(self,brand, model)

            def brand_name(self, brand):
                print('base class method')
                self.brand = brand
                print(brand +" " +'is a good brand')

            def Vehicle_model(self, model):
                print('base class method')
                self.model = model
                print('i am a '+ ' '+ self.model +''+self.brand+"model") 


     maz = Mazda(2019,'camry')   
     output>>>   Am a vehicle

     maz.brand_name('Toyota')
     output>>> vehicle class
     output>>  'Toyota'

i want override the method in the base class but when i try using the derived class object to call the method inside the derived class, it not working to my expectation. i expect the derived class methods to be executed not the base class, please guys what am i doing wrong

Upvotes: 0

Views: 95

Answers (2)

Allaye
Allaye

Reputation: 950

class Vehicle():

    def __init__(self, brand, model):

        print('Am a vehicle')

    def brand_name(self, brand):
        print('vehicle class')
        self.brand = brand
        return brand


    def Vehicle_model(self, model):
        print('vehicle class')

        self.model = model
        return model


 class Mazda(Vehicle):
        def __init__(self, model, brand):
            Vehicle.__init__(self,brand, model)  # your problem was here wrong indentation

        def brand_name(self, brand):
            print('base class method')
            self.brand = brand
            print(brand +" " +'is a good brand')

        def Vehicle_model(self, model):
            print('base class method')
            self.model = model
            print('i am a '+ ' '+ self.model +''+self.brand+"model") 

do well and check your indentation, that seems to be the problem, Vehicle.__init__(self,brand, model) was not indented well. hope it helps

Upvotes: 0

Dipen Dadhaniya
Dipen Dadhaniya

Reputation: 4630

Try the code after proper indentation:

class Vehicle():

    def __init__(self, brand, model):

        print('Am a vehicle')

    def brand_name(self, brand):
        print('vehicle class')
        self.brand = brand
        return brand


    def Vehicle_model(self, model):
        print('vehicle class')

        self.model = model
        return model


class Mazda(Vehicle):
    def __init__(self, model, brand):
        Vehicle.__init__(self,brand, model)

    def brand_name(self, brand):
        print('base class method')
        self.brand = brand
        print(brand +" " +'is a good brand')

    def Vehicle_model(self, model):
        print('base class method')
        self.model = model
        print('i am a '+ ' '+ self.model +''+self.brand+"model") 


maz = Mazda(2019,'camry')

maz.brand_name('Toyota')

It is working fine.

Upvotes: 1

Related Questions