Type inheritance
Type inheritance is the ability of a class to inherit a interface (interface - in this context - does not necessarily mean something that makes use of keyword interface
, but rather all that is made public by the class") and thus be referenced by this other type. Example:
Abstract class Animal:
public abstract class Animal {
protected String especie;
public abstract boolean nascer();
public abstract boolean morrer();
}
Interface Movel:
public interface Movel {
public void andar();
}
Classe Pessoa:
public class Pessoa extends Animal implements Movel {
@Override
public boolean nascer() {
//alguma lógica aqui
return false;
}
@Override
public boolean morrer() {
//alguma lógica aqui
return false;
}
@Override
public void andar() {
//alguma lógica aqui
}
}
In the example above, we can reference the class Animal
through various types:
//Como Pessoa
Pessoa pessoa = new Pessoa();
//Como Animal
Animal animal = pessoa;
//Como Movel
Movel movel = pessoa;
//Como Object
Object objeto = pessoa;
Since Java allows a class to implement multiple interfaces, we can state that it supports multiple type inheritance.
State heritage
State inheritance is the ability to inherit the state of other classes through their attributes. In the example above, we can see an example of this where Pessoa
inherits the attribute especie
abstract class Animal
through a simple inheritance.
Multiple inheritance of state would be the ability to directly inherit the state of multiple classes. This type of inheritance has some problems such as: "What to do when classes have methods, constructs or attributes with the same name? Who takes precedence?". Java does not support multiple state inheritance to avoid problems arising from such an approach.
Multiple inheritance of implementation
In addition to the asked inheritance types, there is another supported by the Java language, the multiple inheritance of implementation through the default methods present in the language since Java 8. This kind of inheritance generates a problem similar to that of multiple state inheritances, since different interfaces may have implemented methods with the same signature.
When a situation like this occurs in Java, the class that implements these interfaces is obliged to implement the method to end ambiguity.
Reference.
Are you sure the term is this? Do you have any context? I’ve never seen "state heritage" and even "type inheritance" in general is not a good term.
– Maniero
It is. In the middle of a simulated fell something that I never saw during the study.
– Murillo Goulart