Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is standard for Interface naming in java api

Tags:

java

I am asked by one of colleague about the Throwable class in java API.

As per standard, I do understand, every word ending *able is a interface in java API. There is a industry standard about using such words as Interface names. So, I unknowingly, told him about this as base interface for all the exception and error types in java world. Then he shows me the java file for this class.

My questions:

  1. Why java people has choosen this name to be a class. I think this should have been a interface by default?

  2. Is this a pattern to use *able words as interface?

  3. Is there any other example of class ending with *able?

Regards.

like image 302
vijay.shad Avatar asked Oct 15 '10 04:10

vijay.shad


3 Answers

Nouns are always used to name classes, but Throwable is an exception.

(See what I did there?)

like image 160
Daniel Alexiuc Avatar answered Sep 24 '22 19:09

Daniel Alexiuc


It's very common for those '-able' names to be interfaces in Java, but there is no official convention for interface naming that I've found that suggests that '-able' names should be interface names, though typically that is the case.

Official Java naming conventions can be found here - it's pretty lean, there really aren't any restrictions for class or interface naming:

  • http://www.oracle.com/technetwork/java/codeconventions-135099.html#367

As to your Throwable question, James Gosling once answered why it's a class rather than an interface, even though the name was more fitting for an interface.

Unfortunately, the original article from Sun/Oracle's site has vanished into the internet ether, so I can only provide indirect attribution:

  • http://c2.com/cgi/wiki?JavaExceptionQuestion
  • http://www.ibm.com/developerworks/forums/thread.jspa?threadID=58994&tstart=45

edit: Since I continue to get upvotes to this question, I found the link to the Sun discussion via the Wayback Machine, here: http://web.archive.org/web/20071013225816/http://java.sun.com/features/2002/03/gosling.html?source=jdc_news&date=20020430

JDC: Why is Throwable not an interface? The name kind of suggests it should have been. Being able to catch for types, that is, something like try{}catch (), instead of only classes. That would make the Java programming language much more flexible.

JG: The reason that the Throwable and the rest of those guys are not interfaces is because we decided, or I decided fairly early on. I decided that I wanted to have some state associated with every exception that gets thrown. And you can't do that with interfaces; you can only do that with classes. The state that's there is basically standard. There's a message, there's a snapshot, stuff like that that's always there. and also, if you make Throwable an interface the temptation is to assign, to make any old object be a Throwable thing. It feels stylistically that throwing general objects is probably a bad idea, that the things you want to throw really ought to be things that are intended to be exceptions that really capture the nature of the exception and what went on. They're not just general data structures.

like image 26
逆さま Avatar answered Sep 24 '22 19:09

逆さま


There are others such as

  • Activatable
  • Observable

And of course there are plenty of interfaces that don't end in -able. Some people like to prefix all their interface names with an 'I' (IAdjustable instead of Adjustable). Like code formatting wars, their isn't universal agreement. Sun has some suggestions but they are pretty vague.

like image 40
Brad Mace Avatar answered Sep 24 '22 19:09

Brad Mace