Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does java.util.concurrent.TimeUnit.convert throw an AbstractMethodError instead of being abstract

java.util.concurrent.TimeUnit has this source:

public long convert(long sourceDuration, TimeUnit sourceUnit) {
    throw new AbstractMethodError();
}

Why isn't this an abstract method like

abstract int excessNanos(long d, long m);
like image 612
keon gao Avatar asked Apr 21 '16 07:04

keon gao


1 Answers

Single line comments above the method declaration says following,

// To maintain full signature compatibility with 1.5, and to improve the
// clarity of the generated javadoc (see 6287639: Abstract methods in
// enum classes should not be listed as abstract), method convert
// etc. are not declared abstract but otherwise act as abstract methods. 

Here, 6287639 is bug id which says,

JDK-6287639 : Abstract methods in enum classes should not be listed as abstract

Now consider the following enum considering it as class and every enum constant as an Object, it is clear that if we create Object of something abstract we must provide implementation and to avoid this convert is not abstract,

enum Blocks {
    A1, B1, C1;
    // It will say enum constant must implement 
    // the abstract method test
    abstract int test();
}
like image 111
akash Avatar answered Oct 19 '22 09:10

akash