0

So I have this plain code here and I'm trying to make sense of these but I end up with a lot of errors and such. The main thing I need to do is to declare a Person class with a Hello abstract method, and then declare a Student class that is derived from the Person class and overrides its Hello method.

here is the code below:

public class Person{
  public abstract void Hello();
}
  class Student: Person {
    public override void Hello() {
 Console.Write("Hello there");
}}
  • 1
    "but I end up with a lot of errors" Can you show the error messages? – Sweeper Jan 09 '21 at 02:57
  • main.cs(11,26): error CS0513: `Person.Hello()' is abstract but it is declared in the non-abstract class `Person' main.cs(10,14): (Location of the symbol related to previous error) these are the errors. I ran them on onlinegdb – Stellaris Ghost Jan 09 '21 at 03:05

1 Answers1

2

Compiler Error CS0513

'function' is abstract but it is contained in nonabstract class. A method cannot be an abstract member of a nonabstract class.

Key is A method cannot be an abstract member of a nonabstract class

You need make Person class an abstract class

public abstract class Person
{
    public abstract void Hello();
}
Fabio
  • 31,528
  • 4
  • 33
  • 72
  • so what should i replace? – Stellaris Ghost Jan 09 '21 at 03:18
  • @StellarisGhost you don't replace anything. Fabio already gave you the answer...mark your `Person` class with the abstract keyword. Your code will compile just fine after that. – David L Jan 09 '21 at 03:20
  • I marked the person class but, it showed 1 error. error CS5001: Program `a.out' does not contain a static `Main' method suitable for an entry point so do I need to declare a Main method? – Stellaris Ghost Jan 09 '21 at 03:26
  • also, thank you for the help, you guys. :) – Stellaris Ghost Jan 09 '21 at 03:26
  • Really :) [Compiler Error CS5001](https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs5001#:~:text=This%20error%20occurs%20when%20no,such%20as%20lower%2Dcase%20main%20.) – Fabio Jan 09 '21 at 03:44