How to implement an abstract method in a child class?

2

I abstracted a method from a class also abstract.

But I'm not sure how to implement this method in the child class.

Here's my code, what do I get?

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 resolve and fix these errors?

link

    
asked by anonymous 09.04.2014 / 22:46

2 answers

4

The base class method can not be called since it has no implementation in the base class.

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

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 instead of abstract:

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
    }
}
    
09.04.2014 / 22:52
1

Does not implement inherited abstract

Deploy Depositar to ContaPoupanca :

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

Can not call an abstract member

The message says it all. As in the base class the method is abstract, there is nothing to be called. So you can not use base .

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

    
09.04.2014 / 22:50