Why would one declare a Java interface method as abstract?
According to the Java Language Specification, the abstract
keyword for interfaces is obsolete and should no longer be used. (Section 9.1.1.1)
That said, with Java's propensity for backwards compatibility, I really doubt it will ever make a difference whether the abstract
keyword is present.
what is a abstract method on a interface in java
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.
Do we need to declare interface methods as abstract
You don't have to, every interface method is implicitly abstract
. It will not be a mistake to write it though.
Why declare an interface as abstract?
Where did you come across the chunk of code you have posted, any old java code base ?
This is what the JLS has to say :
9.1.1.1 abstract Interfaces:
Every interface is implicitly abstract. This modifier is obsolete and should not
be used in new programs.
9.4 Abstract Method Declarations:
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.
Creating an interface with an abstract method
JLS says this:
9.4 Abstract Method Declarations:
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.
You also don't need to specify public
for interface methods.
See the documentation for Defining an Interface:
All abstract, default, and static methods in an interface are
implicitlypublic
, so you can omit the public modifier.
public interface Payable
{
double getPaymentAmount();
}
You'll need an implementation of the interface to actually implement the logic. For example something like this:
public class PayableImpl implements Payable
{
double getPaymentAmount()
{
// actual implementation that returns the payment amount
}
}
Creating new Abstract Method vs Interface Method
I think the wording of the question is too abstract to give a definite yes or no answer.
In general there's nothing wrong with abstract classes. The trouble is when people misuse them. How do we define misuse? Or rather, what's the proper use of abstract classes?
For this we have to remember that OO is a way of modelling the "real world": good designs give easy to understand models, bad designs are hard to follow. Moreover, good OO designs can be extended in the directions the modelled problem is likely to extend too. (I'm not talking about the extends
keyword here obviously.)
What an abstract method says is this: the other methods declared in this class make no sense without this one. Whereas what an interface says is that to be an X you need at least these methods.
So while the abstract method is a way to express demands for an implementation, an interface defines a role that anyone implementing it can play.
Sometimes you need one, other times you need the other.
In Java, when should I use an abstract method in an interface?
There isn't any functional difference. No implementation is ever provided in a java interface so all method declarations are implicitly abstract.
See [1]: http://java.sun.com/docs/books/tutorial/java/IandI/abstract.html
A direct quote form the above:
Note: All of the methods in an interface (see the Interfaces section) are implicitly abstract, so the abstract modifier is not used with interface methods (it could be—it's just not necessary).
Declaring an abstract interface in Java may add some additional meaning to that interface?
Its usage is marked "obsolete and should not be used" in JLS 9.1.1.1, abstract
Interfaces.
IMO it's also misleading, because of its redundancy--it implies something different than an interface that isn't marked abstract, but all interfaces are implicitly abstract
.
This SO question discusses a bit further.
Related Topics
Differencebetween Synchronized on Lockobject and Using This as the Lock
Apache Httpclient 4.0.3 - How to Set Cookie with Sessionid for Post Request
Why in Java Enum Is Declared as Enum<E Extends Enum<E>>
Find Maximum, Minimum, Sum and Average of a List in Java 8
What Is the Purpose of Accesstype.Field, Accesstype.Property and @Access
Is There a Commonly Used Rational Numbers Library in Java
How to Determine the Ip of My Router/Gateway in Java
Writing in the Beginning of a Text File Java
How to Disable Loggers of a Class or of Whole Package
What Is an Instance Variable in Java
How to Unzip Files Recursively in Java
How to Convert Int[] to Byte[]
How Are Servlet Url Mappings in Web.Xml Used
Maximum Size of a Method in Java
How to Check a Timeperiod Is Overlapping Another Time Period in Java