Abstract method in Interface

Why are we able to define abstract method in an Interface?

I don't imagine when I must use an abstact method in a interface.

Does anybody ever use this?

[172 byte] By [enrico.classa] at [2007-11-27 10:21:05]
# 1

Every method in an interface is abstract (and public).

The following are all the same, if they're in an interface:

void foo();

public void foo();

abstract void foo();

public abstract void foo();

The first form is preferred, since the others all carry redundant information.

Abstract simply means that the method is not implemented here, but it's part of the type--the contract--and any concrete subclass must have an implementation. This is true both in abstract classes and in interfaces.

jverda at 2007-7-28 17:07:53 > top of Java-index,Java Essentials,Java Programming...
# 2

You have no choice but to use an abstract method in an interface. Concrete methods are not permitted on an interface

I wonder, are you actually referring to the use of the "abstract" modifier on methods in an interface? That's superfluous, yes. I see this all the time, and the argument is that it clarifies for other developers that methods on the interface are abstract, but if a Java developer doesn't already understand that all methods on an interface are implicitly abstract, and doesn't understand that a method without a body must be abstract, I don't see what value that clarity provides anyway

georgemca at 2007-7-28 17:07:53 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks for your answer.

I can declare an abstract class which implementes an interface.

The abstract class doesn't require the implementation of the methods

declared in the interface.

Why this?

I don't understand because an abstract class can implements an interface!

enrico.classa at 2007-7-28 17:07:53 > top of Java-index,Java Essentials,Java Programming...
# 4

> Thanks for your answer.

>

> I can declare an abstract class which implementes an

> interface.

> The abstract class doesn't require the implementation

> of the methods

> declared in the interface.

>

> Why this?

Because the class is abstract.

A concrete class must provide (or inherit) implementations for all abstract methods in all its ancestor classes and for all methods in all interfaces it implements (and remember, all methods in an interface are abstract).

If a class does NOT have an implementation for one or more of those methods, the class must be declared abstract to indicate that.

> I don't understand because an abstract class can

> implements an interface!

Any class *can* be declared abstract (except final classes, I think).

Any class that doesn't have implementations for all its methods (including those from interfaces) *MUST* be declared abstract.

jverda at 2007-7-28 17:07:53 > top of Java-index,Java Essentials,Java Programming...
# 5

You're actually imagining it to be more complicated than it really is.

jverda at 2007-7-28 17:07:53 > top of Java-index,Java Essentials,Java Programming...