How to implement an abstract method in a daughter class?

Asked

Viewed 2,412 times

2

I abstracted a method from an abstract class as well.

But I’m not sure how to implement this method in the daughter class.

Here’s my code, what I fix?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Banco_Exercicio1
{
    public class Cliente
    {


        public string Nome { get ; set ;}
        public string Rg { get ; set ;}
        public string Cpf {get ; set ;}

        public Cliente(string nome)
        {            
            this.Nome = nome;
        }
    }

    public abstract class Conta
    {
        public Cliente Titular { get; set; }
        public double Saldo { get; set; }
        public int Numero { get; set; }

        public Conta(Cliente nome)
        {
            this.Titular = nome;
        }

        public Conta() {}

        public abstract void Depositar(double Valor) ;


        public abstract void Saca(double Valor);   

    }

    public class ContaPoupanca : Conta // Essa linha apresenta erro: does not implement inherited abstract member
    {
        public ContaPoupanca(Cliente nome) : base(nome) { }

        public ContaPoupanca() {}

        public override void Saca(double Valor)
        {
            base.Saca(Valor + 0.10);     //Error: Cannot Call an abstract member
        }      
    }

    public class ContaCorrente : Conta
    {
        public ContaCorrente(Cliente nome) : base(nome) { }

        public ContaCorrente() {}

        public override void Saca(double Valor)
        {
            base.Saca(Valor + 0.05);    //Error: Cannot Call an abstract member
        }

        public override void Depositar(double Valor)
        {
            base.Depositar(Valor - 0.10);   //Error: Cannot Call an abstract member
        }
    }

    public class TotalizadordeContas 
    {
        public double ValorTotal {get ; private set ;}

        public double Soma (Conta conta)
        {
            ValorTotal += conta.Saldo;
            return ValorTotal; 
        }
    }

}

How to fix and fix these mistakes?

http://social.technet.microsoft.com/Forums/pt-BR/054bda3b-40a6-4723-82ba-7c119ffdccd3/como-implementar-um-mtodo-abstrato-em-uma-classe-filha?forum=cermicrosoftpt#054bda3b-40a6-4723-82ba-7c119ffdccd3

2 answers

4

The base class method cannot be called because it has no implementation in the base class.

The call to the base class method should be made when the method is virtual in the base class, not when the method is abstract.

By analyzing your code: From what I understood from your code, the base class should have some responsibility, so the method should be implemented in the base class as well, as a virtual rather than abstract method:

Suppose the base class has the responsibility to write to the database. In this case you could do so:

public abstract class Conta
{

    public Cliente Titular { get; set; }
    public double Saldo { get; set; }
    public int Numero { get; set; }

    public Conta(Cliente nome)
    {
        this.Titular = nome;
    }

    public Conta() {}
    public virtual void Depositar(double Valor)
    {
        // exemplo: gravar o valor depositado no banco de dados
        // usando o seu ORM
    }


    public virtual void Saca(double Valor)
    {
        // exemplo: gravar o valor sacado no banco de dados
        // usando o seu ORM
    }
}

1

Does not implement inherited Abstract Member

Implement Depositar in ContaPoupanca:

public override void Depositar(double Valor) { ... }

Cannot call an Abstract Member

The message already says it all. As in the base class the method is abstract, it has nothing to be called. Therefore, you cannot use base.

As @Miguelangelo said, if the goal is for the base method to be called, use virtual.

Browser other questions tagged

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