• 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

Extending the Oracle supplied Interface

 
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys and Gals, I hope you're having a nice weekend

I am implementing the threading pat of B&S at the moment and I came across an interesting post by the excellent and helpful rancher Roel De Nijs. The quote is from Robertos tests for locking thread. The quote, if I read it correctly seems to suggest that it is valid to create your own interface which extends the Oracle provided interface, modify the methods you feel need modifying and then implement your newly written Interface and have no more dealings with the Oracle interface. Am I losing it or is that permitted?

Roel said:

.... And I would take it 1 step further: update/delete/unlock should NEVER throw a RNFE, because you have to lock a record and after successfully locking it, another thread can't do anything with it (so certainly not deleting), otherwise there would be something wrong with your locking mechanism. That's why I created an own interface (extending Sun's interface of course) and I redefined these 3 methods without the throws RNFE clause (and of course I added an explanation in my choices.txt about it).

Kind regards,
Roel

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Glen Iris wrote:an interesting post by the excellent and helpful rancher Roel De Nijs


It's always nice to get a compliment. That's what keeps us running (and of course all the free beer bartenders get in the saloon )

Glen Iris wrote:Am I losing it or is that permitted?


It all depends on what you want to do

1. It's completely legal to extend the given interface and have your own interface with some extra methods (e.g. an extra find-method which returns record numbers & records). If your Data class implements your own interface, it will also implement the given interface and you are not violating any must requirement

2. You can also redefine 1 or more existing methods (the ones you get from the given interface), but you still have to make sure that you keep valid overrides (otherwise your program will not compile). E.g. the update-method in my interface has exactly the same signature as in the given interface except for the throws-clause. Because a record must be successfully locked before it can be updated, a RNFE can only be thrown by the lock-method, so for the update-method that's never going to happen, so I removed this throws-clause (and this decision was of course well-documented in my choices.txt)

3. you still have to provide an implementation for every method in the given interface. So you can't add a new create-method for example (with a signature you prefer more than the given one) and implement your create2-method and throw an UnsupportedOperationException from the original create-method. That's risky business and could result in (automatic) failure.

Good luck!
 
Glen Iris
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Glen Iris wrote:an interesting post by the excellent and helpful rancher Roel De Nijs


It's always nice to get a compliment. That's what keeps us running (and of course all the free beer bartenders get in the saloon )

Glen Iris wrote:Am I losing it or is that permitted?


It all depends on what you want to do

1. It's completely legal to extend the given interface and have your own interface with some extra methods (e.g. an extra find-method which returns record numbers & records). If your Data class implements your own interface, it will also implement the given interface and you are not violating any must requirement

2. You can also redefine 1 or more existing methods (the ones you get from the given interface), but you still have to make sure that you keep valid overrides (otherwise your program will not compile). E.g. the update-method in my interface has exactly the same signature as in the given interface except for the throws-clause. Because a record must be successfully locked before it can be updated, a RNFE can only be thrown by the lock-method, so for the update-method that's never going to happen, so I removed this throws-clause (and this decision was of course well-documented in my choices.txt)

3. you still have to provide an implementation for every method in the given interface. So you can't add a new create-method for example (with a signature you prefer more than the given one) and implement your create2-method and throw an UnsupportedOperationException from the original create-method. That's risky business and could result in (automatic) failure.

Good luck!



Thanks yet again Roel. It was almost too good to be true until I saw point 3! I was planning on creating a new update method and pass it a Contractor object instead of a String array.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Glen Iris wrote:I was planning on creating a new update method and pass it a Contractor object instead of a String array.


You could try that, but I'm afraid chances on failure will be big. But if you want to take a risk, just go for it (and let us know how it went).

From a design perspective I would definitely not create such a method, because it makes your interface and Data class dependent on Contractors, so no code reuse at all. My interface & Data class can be (almost) completely reused for handling similar data files containing customer info, hotel info and so on.
 
Greenhorn
Posts: 20
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason mentioned by Roel for creating an extended interface is of course a perfectly valid one, but please don't be fooled (like I was at first) by the notion that you need to extend it to add missing functionality.
I thought the original interface wouldn't suffice since I wanted records to be returned instead of indices, I wanted a book method to be in there, etc.
While in fact the original interface contained all the basic methods I needed for the Bodgitt & Scarper assignment.

I ended up making the search ("find" returning records) and book methods part of the service interface, instead of the data interface.
These methods couldn't care less where methods like "read" and "find" get their data from (RMI, Sockets, DB File, etc).
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for that misunderstanding. Not trying to fool you

Like you said it's perfectly fine to just implement the given interface. It was just something I did because with my given interface I felt there was something missing. Creating your own interface is not a must requirement at all.

And search/book methods should indeed be in a seperate service (that's what I did too) and these methods use the methods from the given interface (and/or your custom interface).
 
Robin van Riel
Greenhorn
Posts: 20
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roel,

No worries, I fooled myself long before I read your post ;).
 
Men call me Jim. Women look past me to this tiny 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