Search code examples
c#inheritanceoverridingabstract-class

dotnetacademy Exercise 7.1 - Inheritence and Abstract Classes with Override?


I am working my way through the C# tutorials on this site, and I am getting stuck on this exercise. I got stuck on 5.1 and a C# programmer I work with discovered that the issue was a freaking space. It's making it hard to see if I am getting the concepts right or not. He said the space issue would never cause an issue compiling in reality, so it may be a bug in the validator on the site.

Anyway, I am on exercise 7.1 here: dotnetacademy exercise 7.1 and I can't seem to get code to validate properly. the steps are as follows:

  1. Create an abstract class named Astrodroid that provides a virtual method called GetSound which returns a string. The default sound should be the words "Beep beep".
  2. Implement a method called 'MakeSound' which writes the result of the GetSound method to the Console followed by a new line.
  3. Create a derived class named R2 that inherits from Astrodroid.
  4. Override the GetSound method on the R2 class so that it returns "Beep bop".

This is the code I wrote up:

using System;

// Implement your classes here.
public abstract class Astrodroid
{
    public virtual string GetSound { get { return "Beep beep"; } }
    
    public void MakeSound() 
    { 
        Console.WriteLine(GetSound); 
    }
    
}

public class R2 : Astrodroid
{
    public override string GetSound { get { return "Beep bob"; } }
}

public class Program
{
    public static void Main()
    {   
        //var MakeSound = new R2();
        //Console.WriteLine(MakeSound.GetSound);
    }
}

The error I get is this:

Not all requirements have been met.

You must define a method named GetSound that returns a string.

Can anyone help me figure out what I'm doing wrong?


Solution

  • You defined GetSound as a property, not a method.

    public override string GetSound() { return "Beep bob"; } is what you wanted.