What is inheritance of type and state?

6

Recently in a Java simulation, I came across a question related to inheritance classification, which I had never seen before.

Taking advantage of the opportunity, I'll separate it into topics, so the answer is clearer:

  • What is inheritance type ?
  • What is state inheritance ?
  • Java implements multiple inheritance of type and or multiple state inheritance ?
asked by anonymous 15.03.2017 / 00:43

2 answers

5

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 .

    
15.03.2017 / 02:15
2

The terms are strange, even more used together.

If I were talking about inheriting the state (attributes) and / or behavior (methods) I would even understand. And Java does both, I think it's even obvious.

If you talk about subclass and subtype (I think the answer there is nicely: P), they are related concepts and end up forming the inheritance as we know it. And yes, Java has both, because the inheritance of these languages presupposes both.

Maybe it's just too bad terms to say that inheritance of a class and inheritance from an interface is supported since normally class has state and interface does not actually have behavior (#), then there is the left-over type for it to "inherit."

    
15.03.2017 / 01:31