0

I am new to C# and have been doing tutorials on .NET Academy. I have been stuck on this exercise and cannot figure out what to do. Here are the requirements:

  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".

And here's what I have so far:

using System;

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


public class R2 : Astrodroid
{
    public override string GetSound { get { return "Beep bop"; } }
    void MakeSound ()
    {
        Console.WriteLine(GetSound);
    }
}

public class Program
{
    public static void Main()
    {

    }
}

Now my problem is I don't know how to make "GetSound" a method without getting compilation errors. How would you go about doing this?

1
  • When you changed GetSound to a method, did you change it to a method in both the abstract and the concrete class (Astrodroid and R1)? Commented Feb 23, 2018 at 0:55

2 Answers 2

2

It looks like you've made GetSound into a Property, rather than a Method. This snippet should get you started without answering the entire tutorial for you.

    public virtual string GetSound()
    {
        return "Beep beep";
    }
Sign up to request clarification or add additional context in comments.

Comments

1

You got it almost right, just do this:

using System;

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

    public void MakeSound()
    {
        Console.WriteLine(GetSound());
    }
}


public class R2 : Astrodroid
{
    public override string GetSound()
    {
        return "Beep bop";
    }
}

public class Program
{
    public static void Main()
    {

    }
}

So you make GetSound a method, override it, and there's no need to override MakeSound since that behavior doesn't change.

1 Comment

That worked! Thank you so much. I actually tried that before but didn't put the extra parentheses in the write line function. lol oh, well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.