*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes a question about the interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "a question about the interface" Watch "a question about the interface" New topic
Author

a question about the interface

yang wulong
Greenhorn

Joined: Jun 07, 2004
Posts: 5
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?
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
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.
    Wickes Potgieter
    Ranch Hand

    Joined: Apr 05, 2002
    Posts: 68
    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?


    Wickes Potgieter<br />SCJP<br />BEA Certified Specialist: Server, Integration, Portal, Tuxedo<br />BEA WebLogic Server 7 Certified Enterprise Developer<br />BEA Certified Architect<br /><a href="http://www.bea.com" target="_blank" rel="nofollow">www.bea.com</a>
    Philippe Maquet
    Bartender

    Joined: Jun 02, 2003
    Posts: 1872
    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

    Joined: Apr 05, 2002
    Posts: 68
    Thank you for your help!
    yang wulong
    Greenhorn

    Joined: Jun 07, 2004
    Posts: 5
    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

    Joined: Jun 02, 2003
    Posts: 1872
    IMO, you can do that.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: a question about the interface
     
    Similar Threads
    JavaDoc for constructors?
    May I refactor provided interface?
    Extending Sun interface
    NX:Am I allowed to modify the "DBMain" interface?
    URLyBird - Extending the DB Interface's Functionality