Steel Plume
Steel Plume

Reputation: 2290

using abstract keyword in interface

I know the difference between "public interface" and "public abstract interface", but when applied on methods are there differences?

public interface IPluggableUi {
    abstract public JComponent getPanel();
    abstract public void initUi();
}

or

public interface IPluggableUi {
    public JComponent getPanel();
    public void initUi();
}

Upvotes: 8

Views: 10040

Answers (5)

Peter Lawrey
Peter Lawrey

Reputation: 533690

A side note: Values defined in an interface are public static final by default so

int VALUE = 5;

is the same as

public static final int VALUE = 5;

in an interface.

Upvotes: 4

Uri
Uri

Reputation: 89799

There is no public abstract interface in Java. All interfaces are "abstract" by the fact that they can't be instantiated. All the functions are automatically "abstract" since they need to be implemented.

Upvotes: 0

Tom
Tom

Reputation: 45144

Methods declared in interfaces are by default both public and abstract.

Yet, one could:

public interface myInterface{
     public abstract void myMethod();
}

However, usage of these modifiers is discouraged. So is the abstract modifier applied to the interface declaration.

Particularly, regarding your question:

"For compatibility with older versions of the Java platform, it is permitted but discouraged, as a matter of style, to redundantly specify the abstract modifier for methods declared in interfaces."

source: http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html

Section 9.4: Abstract method declarations.

Upvotes: 25

Brian Agnew
Brian Agnew

Reputation: 272347

No. You can only apply abstract on a method to an abstract base class.

Interfaces specify the set of methods that have to be implemented ultimately by a concrete (non-abstract class).

Abstract will specify a method which is not implemented in an abstract base class, and that must be implemented in a concrete subclass.

(Note also that the public keyword is superfluous on method specifications on an interface)

Upvotes: 0

IAdapter
IAdapter

Reputation: 64807

no, you could also write

public interface IPluggableUi {
    JComponent getPanel();
    void initUi();
}

its the same thing

Upvotes: 4

Related Questions