Memory organization when there is inheritance

Asked

Viewed 103 times

1

When I declare funcionario as Pessoa in the main(), where the attribute value "goes" to salario, since the class Pessoa does not have that attribute?

public abstract class Pessoa {

    private String nome;
    private Long cpf;

    public Pessoa(String nome, Long cpf) {
        this.nome = nome;
        this.cpf = cpf;
    }
}




public class Funcionario extends Pessoa {

    private float salario;

    public Funcionario(String nome, Long cpf, float salario) {
        super(nome, cpf);
        this.salario = salario;
    }
}



public class Principal {

    public static void main(String[] args) {
        Pessoa funcionario = new Funcionario("Fulano", 123L, 1000);
    }

}

2 answers

5

In this case you have a conceptual error. The Employee class is a specialization of the Person class. This means that the Employee class can and should normally have attributes and methods that are specific to it. This is the case with Salary. If you need this value. You must instantiate an Employee class object and use an Employee type variable. Although you created an instance of Employee you used a Person-type variable for the created object. In this way, the employee variable will only recognize what is part of the definition of Person.

public class Principal {

  public static void main(String[] args) {
    Funcionario funcionario = new Funcionario("Fulano", 123L, 1000);
  }
}

When you do Person employee = new employee("So-and-so", 123L, 1000); the employee variable will only recognize what is part of Person. Although in memory there is the attribute Salary.

A good test is you do the CAST to check this

Funcionario f = (Funionario) funcionario;
System.out.println(f.getSalario());

OBS> Create a getSalario method in the Functio class to check the value.

  • In this case, the instance is of the Employee class, but since the variable type is Person, you cannot access the salary attribute?

  • You can only access the attributes and methods that in this case are defined in the Super Class - which in this case is Person. Test the cast and check. The salary attribute will not be accessible for the Person variable.

  • I did the test and I saw that this is the case. I’m not sure I understood the part "If you need this value. You should instantiate an object from the Employee class and not the Person class.". The function object is no longer an instance of the Employee class?

  • You are correct. I got confused. You must use a variable of type Funcio. The instance is already of Type Funcio.

5

There is the object and there is the variable. There are types that are by value and types that are by reference. In the types by value the object is placed at the location of the variable, and the types by reference they are in another location (typically the heap) and in the variable goes the reference to this object.

When you create an instance of a class, therefore you create an object, of a type, through the class constructor and the new, you are allocating space in memory for the class object being created and initiating the values of that object. This is what you will have in memory, an object of the class indicated in the constructor. So that’s where the fields go (does not call attribute, I regret that almost everyone learns wrong) class Funcionario, which are the fields of the classes that he inherits most the fields declared in the class itself.

The fact of declaring variable as a more general type does not change the layout memory class sense instantiated, everything is there. Only the reference to the object considers that the type is more general, in the case Pessoa, but the object is integral.

Compiler does not let you access class-exclusive fields Funcionario by the variable funcionario Because they say she’s a guy who doesn’t have those camps, even if they’re actually there. Nothing prevents you to access the fields of this same object through another variable that is of the most specialized type, making a cast.

Another common mistake is to make this inheritance. The bulk of the problems that people commit in object orientation is with artificial examples or that have always been taught wrong. That is why it is necessary to be questioning and understand why it is so and not just accept that someone said it is so. See more on Official can be the role of Natural Person?.

There are other problems in this code, but it is not the focus of the question.

  • in that case, Person and Official are types by reference ?

  • Yes, all classes are.

Browser other questions tagged

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