What is type and state inheritance?

Asked

Viewed 820 times

7

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

Taking the opportunity, I will separate into topics, so that the answer is clearer:

  • What is type inheritance?
  • What is state heritage?
  • Java implements type multiple inheritance and or multiple inheritance of state?
  • 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.

  • It is. In the middle of a simulated fell something that I never saw during the study.

2 answers

7


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.

3

The terms are strange, even more used together.

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

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

Maybe it’s just too bad terms to say that there is support for inheritance of a class and inheritance of an interface since usually class has been and interface has neither, actually nor behavior (except Java 8 that changed the interface semantics), so that leaves the guy for "inherit".

  • 1

    That, state has to do with class anyway. After more searches I found this link from Oracle that mentions "state inheritance": https://docs.oracle.com/javase/tutorial/java/IandI/multipleinheritance.html

Browser other questions tagged

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