So here I was trying this "Effective Java 2nd edition" exercise about writing extensible enums.
I faced a really strange issue though - I had each of the enum instances implement the interface methods and all looked good in eclipse. But when I tried to compile it through maven, it failed, although eclipse didn't throw any compilation errors previously at all (both my eclipse and maven use the same JDK: 1.6.0_33-b05
). It turned out that I had to override the interface method(s) [I'll call it method X from now] inside the enum (outside of all enum instances) too for fixing this!
Here's a sample explaining this:
Interface:
public interface IDay
{
public boolean isHoliday();
}
Enum:
public enum Day implements IDay
{
SATURDAY
{
@Override
public boolean isHoliday()
{
return true;
}
},
SUNDAY
{
@Override
public boolean isHoliday()
{
return true;
}
},
MONDAY
{
@Override
public boolean isHoliday()
{
return false;
}
};
// Method X: the project won't compile for me without this method!
public boolean isHoliday()
{
return false;
}
}
Invocation:
public class DayTester
{
public static void main(final String[] args)
{
// line Y: this line won't maven compile if I got rid of the method X
System.out.println("Sunday: " + Day.SUNDAY.isHoliday());
}
}
Strange enough, eclipse was completely ok without the method 'X' yet the maven compilation would fail on the line Y saying
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project X: Compilation failure [ERROR] DayTester.java:[7,46] cannot find symbol [ERROR] symbol : method isHoliday() [ERROR] location: class Day
And my eclipse save actions automatically inserts @Override if I had the method X in place. Removing the method X throws compilation errors in my eclipse aon those Override annotations I had previously.
Here are my questions: 1. Why won't maven compile in this case when eclipse does? 2. What do the Override annotation errors mean here? 3. Is the method X even accessible somehow? What am I missing to understand?