Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python (retired) Inheritance Override Inherited Methods

bummer expected "Meeeeeeeeeeeeeeeeeeeeeeeee" got meeeeeeeeeeeeeeeeeeeeeeee

code won't pass challenge the response l'm getting is confusing me help. thank you

sheep.py
from animal import Animal

class Sheep(Animal):
  sound = "meeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"

  def animal_noise(self):
    return self.sound.lower()

  def Sheep_noise(self):
    return self.sound.uppercase()

2 Answers

The last step of that challenge requires you to override the noise() method in the Animal base class. You do that by defining a method with the exact same name in the Sheep class. The code should look like the following:

from animal import Animal

class Sheep(Animal):
  sound = "meeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"

  # let's re-define the noise method and override the 
  # implementation from the base class
  def noise(self):
    # we need to transform the "sound" instance var into it's upper-case version
    return self.sound.upper()

Note that in order to transform a string into its uppercased counterpart you need to call the upper() method on it.

Hope this helps.

Andrei thank the code passed perfectly fine but l still don't understand why I have to use the name noise not def Sheep_noise(self)

This relates to the way class inheritance works. Let's say you have a Base class with a method called boo and you extend that class into a new class called Derived. The inheritance link established between the two classes automatically grants the Derived class access to the boo method implemented in the Base class. So when you call boo on the Derived object you will actually invoke the boo method implemented by the Base class. This is why it is called inheritance in the first place: The child class gets stuff for free from its parent.

# let's create a base class and define a method on it
class Base:
  def boo(self):
    print("Boo!")

# let's create a derived class and leave it empty
# but we make it inherit from Base
class Derived(Base):
  pass

# create an instance of the Derived class
d = Derived()

# even if we did not explicitly define a boo method
# on the derived class we can call it due to inheritance
d.boo()

# the line above prints Boo! at the prompt.

However, if you want the Derived class to do it's own thing when boo() is invoked on it, you need a mechanism to override (and renounce) the implementation provided by the parent class via inheritance. This process is called method overriding. The child class overrides the implementation provided by the parent.

The majority of Object Orientation languages (not just Python) implement this feature by allowing the child class to define a method with the exact same name as the method you want to override in the parent class. In other words, we do not define a method called Derived_boo like you suggested; you just define a method called boo. This will instruct the Python interpreter to discard the implementation of the boo method provided by the Base class and use the one provided by the Derived class instead.

# let's change the Derived class to override the boo method
class Derived(Base):
  # replace the method with the same name in the Base class
  def boo(self):
    print "Bar!"

# let's call boo again on the Derived object
d = Derived()
d.boo()

# the line above will print Bar! at the console

Hope this helps.

Dan Johnson
Dan Johnson
40,532 Points

In order to override a method of the base class, it needs to have the same name in the child class:

def noise(self):
  return self.sound.upper()

thank you so much it is now very clear

thank you so much it is now very clear

thank you so much it is now very clear