Defining an abstract attribute in a non-extabstracted class

Asked

Viewed 421 times

2

I want to be able to instantiate an employee class (so this can’t be abstract) and have an abstract salary attribute that will be static for each type of employee.

I can do (in the working class):

private abstract int salario;

and in their subclasses:

private static int salario = x,

x being the value I want it to be for each subclass?

  • You are in the stack. Translate your question.

  • 2

    You can’t, but if you explain why you want to do this, it might be possible to find an alternative.

  • You know that if you instantiate Gerente and that if Gerente inherit from Funcionario, then you will be instantiating a Funcionario, check?

1 answer

1

If the intention is to prevent setSalario() you can put the setSalario method as private and call it next to the setCargo method()

Example

public class Funcionario{
    private float salario;
    private String cargo;

    public void setCargo(String cargo){
        this.cargo = cargo;
        setSalario();
    }

    private void setSalario(){
        if(this.cargo.equals("nomeDoCargo"){
            this.salario = <valor>;
        }
        else if(...){
           ...
        }
        ...
    }
}

Thus you protect the salary and only allow you to change if you change the position.

Browser other questions tagged

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