Dmitry Dmitry - 6 months ago 9
Java Question

Is there a way to specify the interface/class being overrode using Java's Override annotation?

Some classes have many interfaces that they inherit from.

I want to make it explicit in my overrided methods what interface or class my method overrides so that when I look back at my code a month from now, I don't have to do a linear search over every interface being implemented and class being extended to find which interface or class the method is being overrode for.

Situation:

class Foo extends Bar implements Foo1, Foo2, Bar1, Bar2
{
public Foo(){}

@Override
void foo1() {}

@Override
void foo2() {}

@Override
void bar1() {}

@Override
void bar2() {}
}


I want to do something like:

class Foo extends Bar implements Foo1, Foo2, Bar1, Bar2
{
public Foo(){}

@Overrides(source="Foo2")
void foo1() {}

@Overrides(source="Bar1")
void foo2() {}

@Overrides(source="Bar2")
void bar1() {}

@Overrides(source="Foo1')
void bar2() {}
}


Is there a way to accomplish this?

Solution (thanks Nikem):

Can create a custom annotation (Overrides.java)

public @interface Overrides
{
String source() default "";
}

Answer

You can create your own annotation specifically for this purpose. Standard @Override annotation does not allow this.

Comments