Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is a abstract method on a interface in java [duplicate]

Possible Duplicate:
Why would one declare a Java interface method as abstract?

I found the following code in one of our ejb interfaces. Does anyone know what the abstract does in the interface? If you do please also explain why it might be needed or provide a reference to read about it =)

@Local
public interface IDomasOrderProcessor {

    public abstract void executeOrderLines(List<OrderLine> lines);
    public abstract void setupJob(List<OrderLine> lines);
    public abstract void setupJob(OrderLine line);
}
like image 372
Marthin Avatar asked Feb 14 '12 13:02

Marthin


People also ask

What is abstract method in interface Java?

An abstract method within an interface is followed by a semicolon, but no braces (an abstract method does not contain an implementation). Default methods are defined with the default modifier, and static methods with the static keyword.

CAN interface have 2 abstract methods?

1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods. Since Java 8, it can have default and static methods also. 2) Abstract class doesn't support multiple inheritance.

Why abstract method is used in interface?

Abstract Classes Compared to Interfaces You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.

Are all methods in an interface abstract?

All methods in an interface are abstract. This statement is True. It is mandatory for an interface to have abstract methods only to apply multiple inheritance.


3 Answers

abstract is redundant in this case. All methods defined on an interface are public and abstract by definition.

Excerpt Java Language Specification section 9.4

Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.

Every method declaration in the body of an interface is implicitly public.

like image 166
Dev Avatar answered Oct 18 '22 15:10

Dev


Both public and abstract modifiers are implicit in interfaces and should be avoided.

like image 38
Tomasz Nurkiewicz Avatar answered Oct 18 '22 13:10

Tomasz Nurkiewicz


A method in an interface is public and abstract by definition. I have heard some people say they feel that explicitly declaring them like that makes it clearer, but to me it seems like extra noise.

like image 41
matt freake Avatar answered Oct 18 '22 15:10

matt freake