0

I'm trying to implement a getter method to get the id of an object: this method should be declared as "public int getId()".

The problem is that the object class extends the Thread class, that already have a "int getId()" method.

When i try to compile i get this error: The return type is incompatible with Thread.getId().

Is there a way to solve this problem (maybe with some kind of annotation)?

Fede Rossi
  • 69
  • 2
  • 7
  • You should change the name of your method. You don't want to be interfering with the Thread method. – carloabelli Jul 29 '14 at 20:54
  • You have a hibernate model class that extends `Thread`? This sounds like a nice design flaw. The only class a model class should extend is an abstract model class. Nothing else. – Tom Jul 29 '14 at 20:57
  • @Tom: where is the design flaw? Please check this question: do you have any comment? http://stackoverflow.com/questions/10616207/work-around-hibernates-errors-in-multithreaded-applications – mark Jul 29 '14 at 21:46
  • For me it sounds like he has a model class managed by hibernate that has a member field annotated with @Id. This class extends `Thread` and he now tries to create a getter method for that entity id which causes the error he mentioned. If this is the case he should try to split the thread class and the model class. If this is not the case, well, then ignore my comment :D. – Tom Jul 29 '14 at 22:00
  • As suggested by Kayaman, impelemt Runnable should solve! – mark Jul 30 '14 at 07:21

2 Answers2

2

No, there's no way to solve that with an annotation. But luckily for you, there's a way to solve that with better design.

Instead of extending Thread implement Runnable instead.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
1

Call the method something else, like getObjectId (depending on what the object is).

For consistency, make sure to rename the setter as well (e.g. to setObjectId).

user253751
  • 57,427
  • 7
  • 48
  • 90