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

Asked

Viewed 637 times

12

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

  • 1

    http://answall.com/questions/22718/java-n%C3%A3o-possui-Heran%C3%A7a-m%C3%Baltipla/89274#89274

3 answers

16

The question posed 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 cannot inherit from various things, that in the background these cases would be better to use composition. And certainly she has her problems.

Well, we have already started to enter into the controversy of inheritance. After years of abuse today we consider the composition rather than inheritance 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 better to avoid it.

If the question is on top of the interface to get the same multiple inheritance result, then the answer is no.

There are better solutions that come closer than multiple inheritance does. Among them are the mixin and the trait. Java 8 has some next to this which are the default methods. These forms try to allow implementations and even state without constituting an inheritance.

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

Since interfaces do not have code, it is insane to say that they help reuse the code. Other mechanisms associated with interfaces can even help (delegation, for example), after all heritage, which I often criticize, is not the only form of reuse of code. But the interface is clearly not one of those ways. Interfaces are just contracts.

Interfaces can help maintain the contract but they can’t have been, they can’t have implementations. So they’re 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 that you as heir have not won anything and will have to turn around to get what you want by following a mandatory.

  • "The interface harms code reuse" - so by "programming by interfaces" I’m giving up, or drastically reducing the possibility, of being able to use my code in other projects?

  • 1

    It’s a little more complicated than that. It harms reuse. It doesn’t make it impossible. The problem is that it doesn’t have code. There you have two options when implementing the interface, either rewrite possibly existing code, or reuse nothing, or create a utility class that contains the standard code needed for the implementation and calls this class within its implementation. There is even a certain reuse, but not automatically, which is the idea of OOP. Interfaces are great, but they don’t solve everything and they’re not problem-free. Look for other questions on the subject or open a new one

  • @bigown You mean then that what harms code reuse is having (or using) only interfaces? Something in the way you wrote made it seem like every use of interfaces causes this.

  • @Pablo Pure interfaces certainly harm the reuse of code. It is obvious that there are cases that they are necessary, this is something else. But every time you choose a pure interface, it has no code, so there’s no reuse, there’s only contract. What promotes reuse is the common heritage of concrete or abstract class or mechanisms that have codes, such as mixins, traits and similar. Among other things that are not considered as object-oriented. Reuse can be obtained in various ways.

  • @That’s right. Thanks for the clarification.

7


Multiple inheritance seeks to solve two problems:

  • to) The ability of a polymorphic object to take the form of objects of distinct hierarchical chains.

  • b) 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 ability b offered by the multiple inheritance is expendable.

Now, if the capacity b is expendable and capacity to can be obtained with interfaces, then interfaces do solve the problem of multiple inheritance, or at least deliver one of the capacities targeted by the multiple inheritance.

Interface with implementations default

The latest version of Java offers the feature of an interface having standard implementation (default methods).

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

Completion

  • In Java, one of the two capacities of multiple inheritance is obtained with the use of interfaces.

  • In Java 8, the two capacities of multiple inheritance are obtained with the use of interfaces.

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

Experience and opinion

Anyway, as I may have already made clear, I don’t see code reuse as a good reason to use inheritance. The ability of an object to take various forms, including distinct hierarchical chains, seems to me much more useful, and this interfaces solve very well.

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

  • You have another interesting question on the same subject, http://answall.com/q/90168/91

4

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

In an attempt to understand how to solve various inheritance problems in Java, this example based on stackoverflow Java Multiple Inheritance illustrates in a classic way the diamond problem and how we can solve it.

Let’s say I have the class Animal that extends bird and horse and that I need to make a class Pegasus, which extends from Ave and Cavalo, Pegasus is a bird and a horse at the same time.
To solve this problem of multiple inheritance, is to cause Pegasus implement 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 of 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 other larger but more explanatory example of the same link mentioned above can help us to understand more closely the question of how the interface can be used to solve the problem of multiple inheritance:

Here we have the animal interface:

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

Interface Bridge extending the Animal interface:

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

Horse interface extending the Animal interface:

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

Here we have the Pegasus interface that extends the Bird and Horse interface:

public interface Pegasus extends Passaro,Cavalo{

}

Abstract class Animalimplementacao 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 Catwalk Abstract class that inherits from Animalimplementacao 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 Cavaloimplementacao that inherits from Animalimplementacao and implements the 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 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 cannot have multiple inheritance at all. Thus, the use of interfaces, abstract classes and concrete classes are components that help solve this type of problem. I hope I helped Duds.

  • 1

    The answer is almost all right. Only the most important part of it is wrong. The first and the last paragraph where it gives the conclusion. The answer proved that the interface does not solve the problem of multiple inheritance. In addition to the interface, it required the use of extra, abstract and concrete classes and manual delegation to solve the problem. Remembering that manual delegation is just what the inheritance tries to avoid. It may have gotten the same result, but the code is huge, there is reuse. And to tell the truth, in this case, the interface is only accessory. The solution is in the classes.

  • The code shown clearly violates DRY making maintenance difficult. http://answall.com/a/22721/101

  • Improved but still concludes that it solves the problem, when it only helps tangentially. She nay solves the problem.

  • I edited the answer and now the conclusion is correct! Thank you.

Browser other questions tagged

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