Type inheritance
Inheritance type is the ability of a class to inherit an interface - in this context - does not necessarily mean something that makes use of keyword interface
, but everything that is made public by the class ") and thus be referenced by that other type.
Abstract Animal Class :
public abstract class Animal {
protected String especie;
public abstract boolean nascer();
public abstract boolean morrer();
}
Mobile Interface :
public interface Movel {
public void andar();
}
Person Class :
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 several types:
//Como Pessoa
Pessoa pessoa = new Pessoa();
//Como Animal
Animal animal = pessoa;
//Como Movel
Movel movel = pessoa;
//Como Object
Object objeto = pessoa;
As Java allows a class to implement multiple interfaces, we can say that it supports multiple inheritance of type
.
Inheritance of State
State inheritance is the ability to inherit the state of other classes through their attributes. In the above example, we can see an example of this where Pessoa
inherits the especie
attribute of the abstract class Animal
through a simple inheritance.
Multiple state inheritance would be the ability to directly inherit multi-class state. This type of inheritance has some problems like: "What to do when classes have methods, constructors, or attributes with the same name? Who has precedence?" Java does not support multiple state inheritance to avoid problems arising from this approach.
Multiple implementation inheritance
In addition to the inherited types of queries, there is another one supported by the Java language, the multiple inheritance of implementation through the default methods in the language since Java 8. This inheritance type generates a problem similar to 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 forced to implement the method to eliminate ambiguity.
Reference .