What is the need for an interface to have abstract methods?

Asked

Viewed 255 times

4

We know that in an interface the methods do not have implementation, only signature, ie only the definition of their methods without the body, we conclude then that all are already methods Abstract, right? But then why are there statements like:

interface Exemplo {
    void umMetodo();

    abstract void outroMetodo();
}


class ImplementaExemplo implements Exemplo {

    @Override
    public void umMetodo() {
        // TODO Auto-generated method stub

    }

    @Override
    public void outroMetodo() {
        // TODO Auto-generated method stub

    }

}

What is the need of an abstract method in an interface? It would have relation exclusively to create the possibility of functional interface?

1 answer

6


We know that in an interface the methods have no implementation

It’s not true.

What is the need for an abstract method in an interface?

None. Totally redundant. Interface methods are abstract, unless explicitly stated as default.

It would have relation exclusively to create the possibility of functional interface?

The convention adopted by some people is that a functional interface should be explicitly declared as abstract, differing from a normal interface. But the only real constraint is that there must be only one method to be used in Amble. I have the impression that the convention began to be used for ignorance of how the mechanism works since they speak in abstract methods.

  • Functional interfaces should have the abstract in the signature? Omitting that then would be an escape from convention such as naming class with lowercase letter?

  • 1

    @Jeffersonquesado gave an upgrade, had mixed things up.

  • In the first position I ended up forgetting the Java 8, my fault, thanks for the alert.

  • @Maniero but I’m still a little confused about the functional interface, in his phrase from the understanding that the interface must have only one method to be declared with a functional interface, correct? So whether the method is explicitly stated as abstract or not, is an interface with a single method a functional interface? Thank you

  • Yes, it is considered, although not guaranteed. See: https://answall.com/q/11162/101

  • Because it is, very strange, in Java itself has the Consumer class ( you can check here: https://docs.oracle.com/javase/8/docs/api/java/util/function/Consumer.html ) even with the annotation @Functionalinterface that has more than one method.

  • 1

    It only has an abstract method.

  • @Emanoel just ratifying what Maniero said: andThen has implementation default, leaving open only the accept, so only this method is abstract

Show 3 more comments

Browser other questions tagged

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