• Post Reply Bookmark Topic Watch Topic
  • New Topic

abstract interface  RSS feed

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

From time to time, I see some interfaces defined as follows:

public abstract interface XXXX {
public void foo();
public void foo2();
}

What is the significance of having the abstract keyword?

Thanks
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
None - it's fully redundant.
 
Victor Ho
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there absolutely no use at all?

I am a little bit surprised that the compiler allows this syntax even if it has no use.

My initial guess was that it enforces there to be an extended interface (i.e. another non-abstract interface extending this abstract interface before a class can implement), but it does not turn out to be the case. So then just a side question, is there any construct in Java that provides such feature?

Thanks
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Yes it is completely redundant and has no use.
The compiler allows it because it is implied.
The above code is better written as:



interfaces are implicitly abstract.
interface methods are implicitly public and abstract.
interface members are implicitly public, static and final.

Java Language Specification (Second Edition) - Chapter 9 for more information.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Victor Ho:
My initial guess was that it enforces there to be an extended interface (i.e. another non-abstract interface extending this abstract interface before a class can implement), but it does not turn out to be the case. So then just a side question, is there any construct in Java that provides such feature?



No. What would it be good for, anyway?
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are lots of cases where Java allows you to optionally use a keyword to make something explicit. For instance:



is the same as:



is the same as:



is the same as:



Also,



is the same as:



I've done some thinking about this, and I think the reason the compiler is flexible in this regard is so that you could code up an interface in the first way, with all of the unnecessary keywords, and then change it to a class during the early design stage without having to touch every method (add public and abstract keywords).

If I choose to code interfaces in the second way, after I write an interface with a couple dozen methods if I decide to change it to an abstract class for some reason, that's a lot more going-back-and-fixing-things. Is this why the good people at Sun made this choice? I doubt it...take the observation for what it's worth.

sev
[ July 18, 2004: Message edited by: sever oon ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


import java.lang.*; // done for you by the compiler if you don't type it in



"Done for you" by the language specification* - the compiler does not add it or have anything to do with it.

*Java Language Specification Second Edition 7.5.3 Automatic Imports
 
sever oon
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A small point of order, Mr. Morris, but a specification can't do anything. All specs can do is say how things should be done. Only code, of which a compiler is comprised, can carry out such operations.

sev
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hence the quotes (your quote) "Done for you".
Perhaps I should have clarified "Done for you [sic]".
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Originally posted by sever oon:

public abstract interface Bar
...

is the same as:

interface Bar
...


This is incorrect. If you do not specify an interface as public, it is limited to the package level.

P.S. Tony, you can delete your posts in the edit window.
[ July 21, 2004: Message edited by: Darin Niard ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!