• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Java annotation for Marker Interface

 
Sendy Yagambrum
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've tried to use annotation to define a marker interface but I think I didn't quite get how to use it. Can someone please help?

I've defined a marker interface using annotation:

@Target(ElementType.TYPE)
public @Interface Searchable{}

and I have defined a class

@Searchable
public class Test{
...
}

and finally there's this piece of code:

ArrayList<Searchable> list = new ArrayList<Searchable>();
Test test = new Test();
list.add(test);

The problem is that the above code does not compile. The compiler does not resolve the type of Test to Searchable. I thought by just putting the annotation @Searchable would automatically make Test to be of that type. Am I wrong?

Thanks,

Sendy
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With annotations, a class does not extend nor implement it. Therefore, there is no IS-A relationship between the Searchable annotation and the Test class. In fact, no class at all can ever implement the Searchable annotation, so nothing can have an IS-A relationship with Searchable (well, except sub annotations).

In your case, Searchable should be a regular interface instead.


Also, I don't think Searchable should be a marker interface at all. Once you have your List<Searchable>, what are you going to do with its contents? Searchable has no methods, so there is not a lot you can do without casting.
 
Sendy Yagambrum
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I understand now. Thanks.

Well, I wanted to use Searchable in this way (for e.g):

public class Test1 implements Searchable
{
// class definition here
}

public class Test2 implements Searchable
{
//class definition here
}

public abstract class AbstractSearch
{
public abstract ArrayList<Searchable> getList();
public abstract void doSomeOperationOnList(ArrayList<Searchable> list);

public void startExecution()
{
ArrayList<Searchable> list = getList();
doSomeOperationOnList(list);
}
}

public class SearchTest1 extends AbstractSearch
{

public ArrayList<Searchable> getList()
{
ArrayList<Searchable> list = new ArrayList<Searchable>();

//execute some query here
while (resultset.next())
{
Test1 test1 = new Test1(resultset);
list.add(test1);
}
return list;
}

public void doSomeOperationOnList(ArrayList<Searchable> list)
{
// iterates over the list and casts each element to Test1
// does something on each Test1 object
}
}

// A similar class : SearchTest2
 
Sendy Yagambrum
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is to say I wanted to use casting. Wouldn't do any good without it, as you said
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic