I can’t reuse child class method

Asked

Viewed 52 times

-1

I created a class ContaCorrente as a daughter of a class Conta.

In class Conta have a method saca:

public boolean saca (double valor) {

    if (saldo >= valor) {

        this.saldo -= valor;
        System.out.println("Operação concluída, seu saldo é: "+ saldo);
        return true;
    } else {

        System.out.println("Seu saldo é insuficiente para essa operação");
        return false;
    }
}

I need to create in the child class a slightly different rule, but reusing this parent class method.

But when I type conta or saca and grip Ctrl+space for the Eclipse to suggest Override method, does not work, does not appear any Override. What I’m doing wrong?

COMPLETE CLASSES:

class Conta {
    private Cliente titular;
    private int AG;
    private int CC;
    private double saldo;
    private static int total;

    public Conta(int AG, int CC) {

        Conta.total ++;
        this.AG = AG;
        this.CC = CC;
        System.out.println("A conta foi criada com sucesso");
        System.out.println("Agência: "+ AG);
        System.out.println("Conta: " + CC);
    }

    public void deposita (double valor) {

        this.saldo += valor;
    }

    public boolean saca (double valor) {

        if (saldo >= valor) {

            this.saldo -= valor;
            System.out.println("Operação concluída, seu saldo é: "+ saldo);
            return true;
        } else {

            System.out.println("Seu saldo é insuficiente para essa operação");
            return false;
        }
    }

    public boolean transfere (double valor, Conta destino) {

        if(this.saldo >= valor) {

            this.saldo -= valor;

            destino.saldo += valor;

            System.out.println("Transação realizada com sucesso, seu novo saldo é: " + this.saldo);
            return true;
        } else {

            System.out.println("Transaão NÃO efetuada, saldo insuficiente!");
            return false;
        }                           

    }

    public double getSaldo() {

        return this.saldo;
    }

    public int getCC() {

        return this.CC;
    }

    public void setCC (int numeroCC) {

         CC = numeroCC;
    }

    public int getAG () {

        return this.AG;
    }

    public void setAG (int numeroAG) {

        AG = numeroAG;
    }

    public Cliente getTitular() {
        return titular;
    }

    public void setTitular(Cliente titular) {
        this.titular = titular;
    }

    public static int getTotal () {

        return Conta.total;
    }
}

DAUGHTER:

public class ContaCorrente extends Conta {
    public ContaCorrente (int AG, int CC) {
        super(AG, CC);
    }
}
  • Post your complete classes.

  • If you type "bag" and Ctrl+space, then it won’t? " account" won’t work because overwrite is in the method, not in the class...

  • even typing bag and Ctrl+space tbm is not going :/

  • to be able to override the class needs to be abstract?

  • I think not, in exercise classes are with certain definitions

  • Create the method in the child class and see if it works. It can be something with auto complete.

  • I thank those who tried to help, but I’ll close the question... 3 people already came to edit the question, one editing what the other already edited... apparently people are more worried about appearing than really helping... Thanks to the 3 who tried.

  • Diego, I tried to manually create the method in the daughter, but when I put @Overrride, the build error :/

  • It doesn’t really need to be Abstract, I tested your classes here the same as it is and it is working yes...

  • 1

    @Jujubadev Editing the question has nothing to do with "wanting to appear". The site is collaborative and editing is part of the ongoing improvement of the content. Read here and here to learn more

  • Both classes are in the same package?

  • They are Diego...

  • If it were in another package you would have problem because you did not set the public modifier in the Account class. Try putting the public in the account class anyway.

  • Yes I imagined, but I checked now and all the classes are in the same pack

Show 9 more comments

1 answer

1


Hello,

Turns out you’re trying to access the attribute saldo mother-class Conta. But, thinking about object orientation and visibility, these attributes can only be accessed by the class itself, since they are private. To change this, you will have to put them as protected (to allow access by the child class and child classes themselves)

Thus:

protected double saldo; (in class Conta)

Now you can generate a method @Override, but if the generate option does not appear, just do it manually (in the class ContaCorrente):

@Override
public boolean saca(double valor) {
    if (saldo >= valor) {

        this.saldo -= valor;
        System.out.println("Operação concluída, seu saldo é: " + saldo);
        return true;
    } else {

        System.out.println("Seu saldo é insuficiente para essa operação");
        return false;
    }
}

Browser other questions tagged

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