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

This question already has answers here:
Closed 11 years ago.
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);
}

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.

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

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.

As per this
document all the methods of interface is public and abstract, so there is no mean to define explicitly abstract method inside the interface.

Related

Difference between using an abstract keyword for a method in interface vs not using the abstract keyword [duplicate]

This question already has answers here:
Why would one declare a Java interface method as abstract?
(4 answers)
Closed 6 years ago.
Say I have a Interface FirstInterface as follows :
public interface FirstInterface {
public void myInterfaceMethod();
}
Here I am declaring a method myInterfaceMethod() which will be defined in the class that will implement this Interface.
but I can also do
public interface FirstInterface {
public abstract void myInterfaceMethod();
}
I have added a keyword abstract in the method declaration.
I want to know, if at all, does it make any difference to add a abstract keyword in the method declaration or not?
All non-default, non-static methods in interfaces are abstract by default. Adding the keyword is harmless, but changes nothing and is discouraged by the JLS "as a matter of style."
From JLS§9.4:
An interface method lacking a default modifier or a static modifier is implicitly abstract... It is permitted, but discouraged as a matter of style, to redundantly specify the abstract modifier for such a method declaration.

Is there a point of marking a method in interface abstract? [duplicate]

This question already has answers here:
Why would one declare a Java interface method as abstract?
(4 answers)
Closed 7 years ago.
I've refactored some methods to an interface and they were left with abstract modifier in the interface declaration. Something like this was in my code:
public interface TestAbstractMethod {
public abstract void doSomething();
}
I noticed it accidentally now and I was actually surprised that the declaration wasn't marked as invalid, that it actually compiles and works.
Is abstract modifier allowed in an interface for a reason? Does this alter the interface's behavior in any way?
Yes, this is allowed, but its use is discouraged in the Java Language Specification, §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.
It is permitted, but discouraged as a matter of style, to redundantly specify the public and/or abstract modifier for a method declared in an interface.

is abstract a must or not?

As you know, in a java interface, all methods have to be defined as abstract. But when I define a method as not typing abstract, the compiler says it is okay. I know that an abstract method must not have a body. Does a method somewhere in an interface necessarily have a name abstract or not? : What i mean is, what is the difference between:
public interface blabla {
public void aMethod();
//or
public abstract void aMethod();
}
No, marking an interface method as abstract has no meaning and is never required.
All interface methods are implicitly abstract (and public too btw).
From the JLS:
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.
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.
It is permitted, but strongly discouraged as a matter of style, to redundantly specify the public modifier for interface methods.
Related question (+ answer with a historical reference to a statement saying that abstract was once required for interface methods):
Java abstract interface
See the sample example below
interface xyz
{
void methodA();
}
Save this to xyz.java
Now compile this using javac tool
and then use the command given belo
javap xyz
the output would be
Compiled from "xyz.java"
interface xyz {
public abstract void methodA();
}
That means when you compile an interface, compiler makes its signature to public and abstract by default.
So it is not necessary to use abstract keyword for any method of interface.
I don't know that they have to be defined as abstract. Probably because they don't. See Oracle's tutorial.
you don't need to specify abstract (default) because within an interface it does not make sense as all the method of the interface needs to be implemented
All methods in an interface are abstract by definition.
You can't create an object out of an interface (e.g., using Interface i = new Interface();) so there's no difference between marking a method as abstract or not.
Any class that implements the interface needs to decide whether to implement it or to let a subclass do it. So as far as the interface is concerned, all methods are abstract by default.
An abstract method provides no implementation. A class which has an abstract method is necessarily abstract, which means that you cannot create instances of this class. To create an instance of that class, you need to subclass and provide non-abstract overwrites for the abstract methods.
An interface never provides an implementation of its methods and it cannot be instantiated. Therefore every method of an interface is per definition abstract. You do not need to provide the keyword abstract when declaring a method in an interface. And by convention the keyword abstract is not used within an interface.
The methods of an interface don't have to be explicitly defined as abstract because they are implicitly abstract and public as defined in the Java Language Specification §9.4. A redundant declaration is perfectly legal though.
If you forgot to put abstract keyword before interface method, Java will implicitly put public abstract keyword before it. Because all interface methods must be abstract.

Why should we declare interface methods as public? [duplicate]

This question already has answers here:
Protected in Interfaces
(15 answers)
Closed 5 years ago.
When I implement an interface method, I am forced to make it a public method.
We may have cases where we want to use either the default (like in case of access within the same package) or protected.
Can anyone please explain the reason behind this limitation?
Interfaces are meant to define the public API of a type - and only that, not its implementation. So any method (or static member) you define in an interface is by definition public.
Since an interface can't contain any concrete implementation, there is no way to call any member methods from within. And declaring such methods but leaving the calls to them to subclasses or totally unrelated clients would mean your type definition is incomplete and brittle. That is why if you need to define protected or package access members, you can do so in an abstract class (which may also contain implementation).
Maybe this will provide some answers.
To my knowledge, you use interfaces to allow people from outside your code to interact with your code. To do this, you need to define your methods public.
If you would like to force someone to override a given set of private methods, you might want to declare an abstract class with a series of abstract protected methods.
An interface is a contract that the class that implements it will have the methods in the interface. The interface is used to show the rest of the program that this class has the methods and that they could be called
EDIT: This answer is meant for C# interface implementations. In this case of Java the scenario is similar just that the syntactic analyzer wants a public keyword mentioned in the interface, which is implicitly done in C#
Interface methods are implicitly public in C# because an interface is a contract meant to be used by other classes. In addition, you must declare these methods to be public, and not static, when you implement the interface.
interface IStorable
{
void Read( );
void Write(object obj);
}
Notice that the IStorable method declarations for Read( ) and Write( ) do not include access modifiers (public, protected ..). In fact, providing an access modifier generates a compile error.
class Document : IStorable
{
public void Read( )
{
//
}
public void Write(object obj)
{
//
}
}
Just think about interfaces as Contracts to be implemented as public
If we mark a interface method as private the implementing class wont
see the method and cant override it.
If we mark a interface method as protected the implementing class
wont see the method unless it is in the same package as the
interface.
If we mark a interface method without any access modifier the
implementing class wont see the method unless it is in the same
package as the interface

Do we need to declare interface methods as abstract [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Java abstract interface
public interface Foo {
abstract public void bar();
}
I guess we don't need to declare abstract as well as public in the above interface. Will the compiler catch this is as a warning or is it allowed by the compiler.
In an interface the the modifiers public and abstract are implied for methods, similarly for fields public static and final are implied. For inner classes static is implied.
It is allowed. public and abstract are automatically added to every interface method.
You don't have to, every interface method is implicitly abstract. It will not be a mistake to write it though.
For interface methods, it is not necessary to declare public and abstract
by default those are public and abstract
It is not necessary but it won't hurt to write it. These modifiers are implied.
I like to do it so everything is explicit and may help other programmers that will work with your code.
You are allowed to declare abstract inside the interface. The complier can pass it.
public interface foointerface {
abstract public void foo();
public void bar();
}
But there is no point to declare in abstract since
we would not implement or allow to implement methods inside interface.

Categories

Resources