Is it correct to say which interface solves the problem of multiple inheritance in Java?

13

It is known that Java does not support multiple inheritance . Is it correct to say that the interface concept solves the problem of multiple inheritance in Java? If so, why?

    
asked by anonymous 27.09.2015 / 05:49

3 answers

6

Multiple inheritance seeks to solve two problems:

    The ability of an object to reuse object code from distinct hierarchical strings.

If you accept the premise that code reuse is the worst case of inheritance use, the b capability offered by multiple inheritance is not required.

Now, if capacity is expendable and ability can be achieved with interfaces, then interfaces instead solve multiple inheritance problem >, or at least deliver one of the multiple inheritance target capabilities.

Interface to implementations default

The latest version of Java provides the interface with a default implementation ( default methods ).

If you could already have a class implementing several interfaces, and if these different interfaces now offer implementations, then in Java 8 the interfaces offer the two capabilities proposed by multiple inheritance.

Conclusion

    In Java , one of two capabilities of multiple inheritance is achieved through the use of interfaces.

    In Java 8 , the two capabilities of multiple inheritance are obtained through the use of interfaces.

Of course, the new interface features in Java 8 have some limitations and are quite cavernous, at least for now that we're not used to.

Experience and opinion

Anyway, as I may have already made it clear, I do not see code reuse as a good reason to use inheritance. The ability of an object to take many forms, including distinct hierarchical strings, seems to me much more useful, and interfaces work fine.

Using only interfaces and very little inheritance I have managed to get simple designs, powerful code and lots of reuse.

    
05.10.2015 / 21:34
15

The question asked in this way is somewhat controversial. The multiple inheritance itself is already controversial, since some people say that it is not even necessary in fact, that things can not inherit from various things, that deep down these cases it would be better to use composition. And certainly she has her problems .

Well, there we started getting into the inheritance controversy. After years of abuse today, better composition than inheritance is considered in most cases. It is said that in real applications and not just in exercises the inheritance brings so many problems in most cases that it is best to avoid it.

If the question is over the interface to get the same result as the multiple inheritance, then the answer is no.

There are better solutions that come closer than the multiple inheritance can. Among them are the mixin and the trait . Java 8 has some near to it that are default methods . These forms try to allow deployments and even state without inheriting.

The interface harms the reuse of code that is the main object-oriented proposition. Of course there is a reason for this. Nobody did it for free. Everything has its cost and its benefit.

Since interfaces have no code, it is insane to say that they help code reuse. Other mechanisms associated with interfaces may even help (delegation, for example), after all inheritance, which I usually criticize, is not the only way to reuse code. But the interface is clearly not one of these ways. Interfaces are just contracts.

Interfaces can help keep the contract but can not have status, can not have implementations. So they are different. They help, but do not replace multiple inheritance.

Interfaces help more in polymorphism. Interface is not inheritance unless you consider inheritance a role that says you as heir did not gain anything and will have to turn around to get what you want by following an obligation.

    
27.09.2015 / 06:10
3

Yes, the interface can solve this problem, but not alone. In addition to the interface, we will need the use of extra, abstract and concrete classes and do manual delegation to solve the problem.

In an attempt to understand how to solve various inheritance problems in Java, this example based on the Java Multiple Inheritance stackoverflow Classically illustrates the problem of diamond and how we can solve it.  Let's say I have class Animal that extends bird and horse and I need to make a Pegasus class that extends from Ave and Cavalo , Pegasus is a bird and a horse at the same time. To solve this multiple inheritance problem, is to have Pegasus implement the bird and horse interfaces.

Example 1
Consider the following interfaces:

public class Passaro implements Avem { }

and

public class Cavalo implements Equo { }

and also

public class Pegasus implements Avem, Equo { }

In order to reduce duplicate code, you can create an abstract class that contains most of the common code for the animals you want to implement.

public abstract class AbstractCavalo implements Equo { }

public class Cavalo extends AbstractCavalo { }

public class Pegasus extends AbstractCavalo implements Avialae { }

Example 2
 This larger yet more explanatory example of the same link cited above may help us to understand more closely how the interface can be used to solve the multiple inheritance problem:

Here we have the animal interface:

   public interface Animal{
        public int numeroDePernas();
        public boolean podeVoar();
        public boolean podeSerMontado();
    }

Passaro interface that extends the Animal interface:

public interface Passaro extends Animal{
    public void fazerCoisasDePassaro();
}

Horse interface that extends the Animal interface:

public interface Cavalo extends Animal{
    public void fazerCoisasDeCavalo();
}

Here we have the Pegasus Interface that extends the Passaro and Horse interface:

public interface Pegasus extends Passaro,Cavalo{

}

Abstract class AnimalImplementation that implements the Animal interface:

public abstract class AnimalImplementacao implements Animal{
    private final int numeroDePernas;

    public AnimalImplementacao(int numeroDePernas) {
        super();
        this.numeroDePernas = numeroDePernas;
    }

    @Override
    public int numeroDePernas() {
        return numeroDePernas;
    }
}

Abstract class PassaroImplementation that inherits from AnimalImplementation and implements the Passaro interface:

public class PassaroImplementacao extends AnimalImplementacao implements Passaro{

    public PassaroImplementacao() {
        super(2);
    }

    @Override
    public boolean podeVoar() {
        return true;
    }

    @Override
    public boolean podeSerMontado() {
        return false;
    }

    @Override
    public void fazerCoisasDePassaro() {
        System.out.println("fazendo coisa de passaro...");
    }

}

Abstract class HorseImplementation inherited from AnimalImplementation and implements Horse interface:

public class CavaloImplementacao extends AnimalImplementacao implements Cavalo{

    public CavaloImplementacao() {
        super(4);
    }

    @Override
    public boolean podeVoar() {
        return false;
    }

    @Override
    public boolean podeSerMontado() {
        return true;
    }

    @Override
    public void fazerCoisasDeCavalo() {
        System.out.println("fazendo coisas de cavalo...");
    }

}

And finally:
Concrete class Pegasus Implementation that implements the Pegasus interface:

public class PegasusImplementacao implements Pegasus{

    private final Cavalo cavalo = new CavaloImplementacao();
    private final Passaro passaro = new PassaroImplementacao();


    @Override
    public void fazerCoisasDePassaro() {
        passaro.fazerCoisasDePassaro();
    }

    @Override
    public int numeroDePernas() {
        return cavalo.numeroDePernas();
    }

    @Override
    public void fazerCoisasDeCavalo() {
        cavalo.fazerCoisasDeCavalo();
    }


    @Override
    public boolean podeVoar() {
        return true;
    }

    @Override
    public boolean podeSerMontado() {
        return true;
    }
 }

In this way, we saw how the interface circumvents the problem of multiple inheritance in java, however, java can not have multiple inheritance in any way, so the use of interface, abstract classes and concrete classes are components that help solve this type of problem. I hope I have helped Duds.

    
27.09.2015 / 17:47