• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to use @Deprecated annotation on an Interface?

 
Ranch Hand
Posts: 337
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have used the @Deprecated annotation on my BaseJDBC interface for two methods. I created a new interface name PreparedBaseJDBC and added an additional parameter ( String [] parms ). I added the new parameter to the two methods and added them to the PreparedBaseJDBC. My class implements PreparedBaseJDBC. I received the error in Eclipse to implement unimplemented methods. When I selected that option it adds the methods marked @Deprecated back to my class.

What am I doing wrong here please?

This is my class. The error says "The type LocalMySQL must implement the inherited abstract method BaseJDBC.executeQuery(String)" and when I choose to fix this by adding unimplemented methods it addes the methods from both interfaces.









 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Deprecated has no effect on how the code is compiled other than to provide a warning, consequently all other code rules apply, such as implementing all methods of an interface in a non-abstract class.
 
Lisa Austin
Ranch Hand
Posts: 337
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:@Deprecated has no effect on how the code is compiled other than to provide a warning, consequently all other code rules apply, such as implementing all methods of an interface in a non-abstract class.



So I am not doing anything wrong? I have to implement the methods which I have marked as @Deprecated? This was just an exercise I had to do but It seems like in the real world if I had to perform this exercise multiple times and I had to implement every method marked as @Deprecated the class will become messy. Is this a best practice or is there a way to get around having to implement methods marked as @Deprecated without having to mark the class as abstract?

 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lisa Austin wrote:
So I am not doing anything wrong? I have to implement the methods which I have marked as @Deprecated? This was just an exercise I had to do but It seems like in the real world if I had to perform this exercise multiple times and I had to implement every method marked as @Deprecated the class will become messy. Is this a best practice or is there a way to get around having to implement methods marked as @Deprecated without having to mark the class as abstract?



Is there a reason you don't just remove the methods from BaseJDBC?

If that's not possible, and you're using Java 8, then you can supply default methods which maybe throw a runtime exception.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At least in Eclipse, and probably in NetBeans, you can create you class with stubs for all the methods you have to implement. It only takes a second.

Then if you want, you can throw some sort of exception if the
 
Lisa Austin
Ranch Hand
Posts: 337
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:

Is there a reason you don't just remove the methods from BaseJDBC?
If that's not possible, and you're using Java 8, then you can supply default methods which maybe throw a runtime exception.



It's part of the exercise I'm working on. The instructions didn't say to remove them. It says to use the @Deprecated.
 
Lisa Austin
Ranch Hand
Posts: 337
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:At least in Eclipse, and probably in NetBeans, you can create you class with stubs for all the methods you have to implement. It only takes a second.

Then if you want, you can throw some sort of exception if the



I'm confused as to why I would do this. Does creating the additional class implement the methods marked as @Deprecated and then I wouldn't have to implement it on the LocalMySQL class? I will check that out.
 
Lisa Austin
Ranch Hand
Posts: 337
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think at this point I'm just confused as to why the @Deprecated is used like this in the exercise. My instructor explains the interface is a contract so I guess this exercise covers a scenario in the real world that I yet don't really understand. I probably should just accept it as it is if what I'm doing is correct which is probably more important.

Thank You all for your help. At least from what I am gathering here I am using this correctly.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lisa Austin wrote:I think at this point I'm just confused as to why the @Deprecated is used like this in the exercise. My instructor explains the interface is a contract so I guess this exercise covers a scenario in the real world that I yet don't really understand. I probably should just accept it as it is if what I'm doing is correct which is probably more important.

Thank You all for your help. At least from what I am gathering here I am using this correctly.



That's sort of the problem with exercises.
They can be a bit abstract (no pun intended).

In the real world code changes. Sometimes a refactoring of an interface or class can result in bits of it being considered no longer useful. However, particularly if this refactoring has been done for a shared package used in a number of projects, there will be code that uses the old, now-deprecated methods. You don't want to have to go through and refactor all those, but you probably also want them to be able to use the nice shiny new stuff.

So you mark the methods, so that people writing new code that uses these classes know not to use those old methods.

I hope that makes some sense and isn't too rambling?
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm confused as to why I would do this... My instructor explains the interface is a contract...


Exactly. The Interface guarantees that certain methods will be available, but it doesn't guarantee how they will be implemented. Therefore you must implement the method, if only to do nothing. Then you mark your methods deprecated to show the user of your code that you don't want these methods used.
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that "deprecated" does not mean "you can't use this", it means "you shouldn't use this". Big difference.
 
Lisa Austin
Ranch Hand
Posts: 337
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You all. I think I understand. Shouldn't vs. Can't. Got it.
 
Look ma! I'm selling my stuff!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic