The response of Anthony Accioly answers the question well, I will complement.
Java has multiple inheritance of method ), but not state ( variable ), via standard methods .
Complete multiple inheritance is almost never required and is almost always wrong , I barely remember the last time I saw a case that needed multiple inheritance, I think it was a biology problem. Reuse techniques are useful, but do not make a type be two complete types, at most having members of two types.
That said, I'm sorry to inform you that almost all examples that teach OOP are weak and wrong and so everyone learns wrong.
Your example is a classic mistake. Professor
is not a Pessoa
and Funcionário
, at most it has members of a Person and a% Employee. Because, contrary to what some people preach, object orientation can not model the real world, it can model an abstraction of the object. real world. Do not understand Person as a human being, but as a token of a person, because that is what this class is. Same as Funcionário
. Employee is an abstract concept created by humans, not being concrete it does not exist in the real world. And it's also a data sheet that a person people can have for a function.
If you wanted to insist on modeling as if it were the real world, it is easy to solve your problem, just hierarchize correctly. Professor
inherits from Funcionário
that inherits from Pessoa
. Your problem is solved right now, because all Professor
is Funcionário
, right? All Funcionário
is Pessoa
, right?
Good, wrong, but the hierarchy is a solution for several cases, not this one.
At most one Professor
can have data and behaviors that all Funcionário
and every Person has. So it makes sense to have interfaces or > traits (which is more or less what Java 8 introduced) or other options , that is, it has these subtypes, but not subclasses. Until mixins may be useful, but Java does not have it then use Delegation .
Even this is often wrong. One person can be Teacher and Student at the same time. A Person can be Funcionário
and Cliente
at the same time, how do you solve this? Simple, make each part independent. A Pessoa
is a person, nothing more than this. An Employee is an employee and a Cliente
is a client, nothing more than this and of course a Professor
is a teacher and Aluno
is a student and nothing more. You do composition between them by doing a association of the activity that each one is exerting.
And I did not even merit that a Cliente
could be Pessoa Física
or Jurídica
.
If you do this inheritance you have more than one person's token in the system, the information is no longer canonical ( DRY of data) and starts causing confusion. It seems to be a public IT thing (homage to Bacco).
Most of the data is related to each other and not inherited. Almost every inheritance should be of subtype, and look at it, and partly of behavior. Almost every inheritance should be abstract class or
27.02.2018 / 15:48