• 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

a question about the interface

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
In the instruction that there is a interface that the Sun want us to implement,I just to ask that can i add more methods in that inteface except the methods the Sun provide to us ?or if I want to add methods I have to extends that interface to make another interface to contain the methods?
 
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yang,

Welcome to JavaRanch and this forum!

In the instruction that there is a interface that the Sun want us to implement,I just to ask that can i add more methods in that inteface except the methods the Sun provide to us ?or if I want to add methods I have to extends that interface to make another interface to contain the methods?



It's normal that you want/need additional methods to the provided interface. But I wouldn't touch the provided one.

There are basically two solutions to that issue:

  • as you suggest yourself, extend the provided interface;
  • simply add methods to the Data *class*.



  • Both solutions are defendable, and - after all - can even be mixed. But I think that added public methods which are not implementation-specific would deserve a place in some extended interface.

    Regards,

    Phil.
     
    Ranch Hand
    Posts: 68
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi,

    I am sitting with the same issue, I am trying to implement the create method but the provided interface only throws DuplicateKeyException. I am not going for the data cache option but instead direct read/write. So, when I want to read the data or update the data in this method, an IOException may occur and I don't know how to throw this to the calling method.

    Any suggestions?
     
    Philippe Maquet
    Bartender
    Posts: 1872
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Wickes,

    I am sitting with the same issue, I am trying to implement the create method but the provided interface only throws DuplicateKeyException. I am not going for the data cache option but instead direct read/write. So, when I want to read the data or update the data in this method, an IOException may occur and I don't know how to throw this to the calling method.



    You've basically three ways of dealing with the IOException issue:

  • chain the IOException in some provided one
  • chain it in some RuntimeException of your own (DataException?) that you throw
  • throw some RuntimeException of your own (DataIOException?).



  • Which solution to choose?

    If you're lucky enough (as me), you can eliminate solution 1 immediately because one of the provided methods where you may encounter some IOException doesn't throw any exception at all (in my case: findByCriteria()), so no exception that you could use as a wrapper.

    So in my case, solution 1 was impossible to implement. But even in the case it'd be possible, I wouldn't have choosen it. Exception chaining is there to *simplify* exceptions handling: gather multiple (possibly many) checked exception types together in some wellknown exception type which is easy to catch. Not only it makes the caller's life easier, but it's indispensable each time you want to publish some generic interface (as no new or broader checked exception may be thrown from a method implemented from an interface - same as the overriding rule). Good example of this: the ServletException in the Servlets/JSP API, a generic checked exception declared to act as a wrapper to any checked exception you'd decide to pass "above" (a SQLException for instance).

    But in our assignment, it's the contrary: we've *multiple* declared checked exceptions (DuplicateKeyException, RecordNotFoundException, SecurityException, ...), all specialized, and mainly one undeclared checked exception: IOException. So, even if findByCriteria() throws some exception in your assignment, it's clear that it's a bad solution: how to wrap in a consistent manner, and how to *document* it, as the wrappers would change from one method to another?

    Now between solutions 2 and 3, I do prefer 2 but it's probably more a question of personal preference.

    Regards,

    Phil.
    [ June 08, 2004: Message edited by: Philippe Maquet ]
     
    Wickes Potgieter
    Ranch Hand
    Posts: 68
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you for your help!
     
    yang wulong
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you for your help,

    I read my instruction again,my instruction say
    "Your data access class must be called"Data.java",must be in a package called"suncertify.db", and must implement the following interface
    package suncertify.db;
    public interface DBMain
    {
    ...
    ...
    }
    because the Sun use a very sensitive word"must"to implement the following interface , so I am still confused that is that mean the sun want Data.java to implement the interface DBMain directly,or for example can I make a interface Databaseinterface to extend the interface DBMain ,then let the Data.java to implement the Databaseinterface.
     
    Philippe Maquet
    Bartender
    Posts: 1872
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    IMO, you can do that.
     
    I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic