Error with Abstract method

Asked

Viewed 43 times

1

Good morning, does anyone know why the compiler keeps accusing the error saying that the method is not declared as Abstract, but even though I have declared?inserir a descrição da imagem aqui

Follow the Repl.it link: https://repl.it/@Wellingtonmazon/Triangle calculus And the codes below:

using System;

namespace triangulo
{
    class Program
    {
        static void Main(string[] args)
        {
            Retangulo r = new Retangulo();
            r.Largura = 8.3;
            r.Altura = 3.5;
            Console.WriteLine("Área do retângulo: {0}", r.CalculaArea());

            Triangulo t = new Triangulo();
            t.Largura = 8.3;
            t.Altura = 3.5;
            Console.WriteLine("Área do triângulo: {0}", t.CalculaArea());

        }
    }
}


using System;
namespace triangulo
{
    abstract class FiguraGeometrica
    {
        public double Largura = 0;
        public double Altura = 0;
        public abstract double CalculaArea()
        {
            return -1;
        }
    }
}


using System;
namespace triangulo
{
    class Retangulo : FiguraGeometrica
    {
        public override double CalculaArea()
        {
            return Largura * Altura;
        }
    }
    class Triangulo : FiguraGeometrica
    {
        public override double CalculaArea()
        {
            return Largura * Altura / 2;
        }
    }
}
  • It seems that the people of the . NET like to err this type of message.

2 answers

1


Because it is an abstract method it should not have implementation, ie the method is only declared but does nothing so that the class that inherit it it is required to implement.

To resolve remove her body, so it stays that way:

    public abstract double CalculaArea();
  • 1

    Thank you very much, I am learning to program. Thank you for helping me.

  • We are under orders!

1

All excerpts from these answers were taken from this source: https://docs.microsoft.com/pt-br/dotnet/csharp/language-reference/keywords/abstract

Since an abstract method statement does not provide any real implementation, there is no method field, the method statement simply ends with a semicolon and there are no keys ({ }) after the signature. For example:

public abstract void MyMethod();  

An abstract class that implements an interface can map interface methods into abstract methods. For example:

interface I
{
    void M();
}
abstract class C : I
{
    public abstract void M();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.