Abstract interface methods?

J

Josef Garvi

I saw today that I can declare an interface method as abstract:

public interface MyInterface {
public abstract void doSomething();
}

What purpose would this serve?
I mean, all methods in an interface are abstract by nature....

--
Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty
 
C

Christophe Vanfleteren

Josef said:
I saw today that I can declare an interface method as abstract:

public interface MyInterface {
public abstract void doSomething();
}

What purpose would this serve?
I mean, all methods in an interface are abstract by nature....

You gave your own answer :)

A method in an interface *is* abstract, since it has no implementation.
A method in an interface is also public by definition.

So when defining an interface, you can leave out those two modifiers, eg:

public interface MyInterface {
void doSomething();
}

will give the exact same results as your example. The one you gave is just
the most verbose :)
 
J

Josef Garvi

Christophe said:
Josef Garvi wrote:

You gave your own answer :)

A method in an interface *is* abstract, since it has no implementation.
A method in an interface is also public by definition.

So when defining an interface, you can leave out those two modifiers, eg:

Thanks! That's what I was suspecting! :)

--
Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty
 
X

xarax

Josef Garvi said:
Thanks! That's what I was suspecting! :)

I always specify "public abstract ..." on the
methods in an interface. It greatly simplifies
cut&paste between an interface and an abstract
class. It also provides better consistency
(aka "readability") when looking at both class
and interface definitions. I don't have to force
myself to remember whether I am looking at a class
or interface, because the declarations are in the
same format. Leaving out the "public abstract"
keywords in an interface is just plain sloppy, IMNSHO.
 
J

Josef Garvi

xarax said:
I always specify "public abstract ..." on the
methods in an interface. It greatly simplifies
cut&paste between an interface and an abstract
class. It also provides better consistency
(aka "readability") when looking at both class
and interface definitions. I don't have to force
myself to remember whether I am looking at a class
or interface, because the declarations are in the
same format. Leaving out the "public abstract"
keywords in an interface is just plain sloppy, IMNSHO.

Good point. But on the other hand, leaving them out gives me a quicker
overview of the methods in the interface, as there is less noise getting in
the way (and in the case of longer declarations, would save it from
wrapping on two lines). Also, when using an IDE like eclipse, I only
declare that my class implements the interface, and let Eclipse take care
of adding the method handlers.

--
Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty
 
R

Roedy Green

Good point. But on the other hand, leaving them out gives me a quicker
overview of the methods in the interface, as there is less noise getting in
the way (and in the case of longer declarations, would save it from
wrapping on two lines).

one the other hand, when it comes time to implement the methods, it is
nice to cut and paste and then just add bodies.

For that it is NICE to have the nugatory public and a NUISANCE to have
the nugatory abstract.
 
C

Christophe Vanfleteren

Roedy said:
one the other hand, when it comes time to implement the methods, it is
nice to cut and paste and then just add bodies.

For that it is NICE to have the nugatory public and a NUISANCE to have
the nugatory abstract.

Any good IDE just adds the methods in the interface to the class the moment
you declare to implement it.

No copy (cutting your interface definitions might not be such a good
idea :) / paste necessary (and source is not always available).
 
T

Tony Morris

Josef Garvi said:
I saw today that I can declare an interface method as abstract:

public interface MyInterface {
public abstract void doSomething();
}

What purpose would this serve?
I mean, all methods in an interface are abstract by nature....

--
Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty

An interface method is implicitly abstract, and public.
An interface member is implicitly public, static and final.

To provide explicit, redundant modifiers to interface methods/members is
poor form (Java Language Specification Second Edition).

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
X

xarax

Tony Morris said:
An interface method is implicitly abstract, and public.
An interface member is implicitly public, static and final.

To provide explicit, redundant modifiers to interface methods/members is
poor form (Java Language Specification Second Edition).

It is poor form to blindly follow Sun's style
recommendations, especially if it's style taken
from their code. Sun got it wrong wrt interface
style.
 
A

Andrew Thompson

It is poor form to blindly follow Sun's style
recommendations, especially if it's style taken
from their code.

I suspect Tony took that from
...where was it, oh yeah..
(Java Language Specification Second Edition)
as opposed to 'code'.
..Sun got it wrong wrt interface
style.

...maybe if they put a gold tinge
around the border, perhaps? ;-)
 
T

Tony Morris

It is poor form to blindly follow Sun's style
recommendations, especially if it's style taken
from their code. Sun got it wrong wrt interface
style.

Agreed.
I do not blindly follow this convention.
I actively follow it, since I strongly agree with it.
Note that this is also a reverse compatibility issue, as well as a
recommended convention.

This suggestion is taken from the JLS, not "code".

-----------------------------------

JLS §9.4
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.

Note that a method declared in an interface must not be declared static, or
a compile-time error occurs, because static methods cannot be abstract.

-----------------------------------


--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
T

Tony Morris

Leaving out the "public abstract"
keywords in an interface is just plain sloppy, IMNSHO.


Quite the contrary.
I disbelieve that this could be the opinion of anyone who is familiar with
earlier versions of Java and/or the Java Language Specification Second
Edition and/or Java Language Specification First Edition.

I refer you to JLS 9.4 (2nd Edition) to understand why this recommendation
by Sun is more than just a convention that you choose to violate (or simply
read the post on the appropriate thread).

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
X

xarax

Tony Morris said:
Quite the contrary.
I disbelieve that this could be the opinion of anyone who is familiar with
earlier versions of Java and/or the Java Language Specification Second
Edition and/or Java Language Specification First Edition.

I refer you to JLS 9.4 (2nd Edition) to understand why this recommendation
by Sun is more than just a convention that you choose to violate (or simply
read the post on the appropriate thread).

There is no reasonable argument from anyone, especially
Sun, that will change my mind on this. I direct all
Java programmers in my shop to follow my style guidelines,
including the "public abstract ..." thing in interfaces.

'nuf said.
 
T

Tony Morris

There is no reasonable argument from anyone, especially
Sun, that will change my mind on this. I direct all
Java programmers in my shop to follow my style guidelines,
including the "public abstract ..." thing in interfaces.

Choose what you will.
I prefer to make conscious decisions knowing the full benefits and drawbacks
of them, whatever they might be.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
C

Chris Smith

Tony said:
I refer you to JLS 9.4 (2nd Edition) to understand why this recommendation
by Sun is more than just a convention that you choose to violate (or simply
read the post on the appropriate thread).

I wonder what you mean. I see that 9.4 says that public and abstract
modifiers are "permitted but [strongly] discouraged", and that it refers
to a reason for *allowing* the abstract modifer (backward-compatibility)
but I see no good arguments for leaving them out. I only see the
statement that they are discouraged. Nor can I think of a reason.

I suspect this is a matter of taste. I tend to include 'public', as a
little protest over the unwise decision to require that interface
methods be public in the first place. I leave out 'abstract'. My
decision is not related to the reasons Roedy gave for that particular
combination, though. I don't see that my decision hurts anyone, and I'm
perfectly content with reading code that has or lacks those modifiers in
whatever combination is desired.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top