This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Adding Methods to the Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Adding Methods to the Interface" Watch "Adding Methods to the Interface" New topic
Author

Adding Methods to the Interface

Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
Hey guys,

Did anyone here add some of their own methods to the interface from Sun/Oracle? Can we do that?

Thanks,
Matt
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
I also added several. Plus I added "RemoteException" to each
throws clause. I'm hoping this is okay. What do others think?

Jim...


BEE MBA PMP SCJP-6
Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
Hey Jim,

I found this post by Roel in regards to this issue:

http://www.coderanch.com/t/522835/java-developer-SCJD/certification/read-method-UB

Not a big deal to fix it, though.

Matt
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5444
    
  13

creating another interface which extends the required-to-implement one and adding a few extra methods is certainly not a problem. I did that (and passed).

But you should NOT change method signatures of given methods, so you can not add a throws-clause with a checked exception. That will result in automatic failure. You can always wrap a possible IOException in a RuntimeException and throw that exception instead. So you don't need to change the method signature


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Robert Benson
Ranch Hand

Joined: Apr 04, 2010
Posts: 56
I also did as Roel did and passed:

  • I left the supplied interface unchanged. You're in trouble if you change it.
  • I added my own methods to an extended interface.
  • I documented my reasons for adding the methods.



  • SCJP 6 , OCMJD 6 ,
    http://www.robertbenson.ie/
    Matt Pavlovich
    Ranch Hand

    Joined: Aug 14, 2010
    Posts: 98
    Roel and Robert,

    Thanks for your input, guys. Two questions, though. Did you format the provided interface with the appropriate Javadoc formatting or did you leave it? Also, with your extended interface, did you then implement both interfaces in your Data class? I am using the Data class as a facade, so all my methods from both these interfaces are in it, which from a test requirement standpoint required me to implement both interfaces (not just the extended one), though from a strictly coding perspective such a move is redundant.

    Thanks,
    Matt
    Matthew Brown
    Bartender

    Joined: Apr 06, 2010
    Posts: 4425
        
        8

    If you extend the interface, then by implementing that interface you automatically implement the DB interface as well.
    Matt Pavlovich
    Ranch Hand

    Joined: Aug 14, 2010
    Posts: 98
    Matthew,

    You are 100% correct. Thus my reason for saying it was redundant to implement both with "implements DB, InterfaceExtension". My issue is with the wording of the exam: "Your data access class must be called "Data.java", must be in a package called "suncertify.db", and must implement the following interface: ". So yes, technically, when I implement my extended interface, the one from Oracle is automatically implemented. However, I don't want to be automatically failed because my project lacks " implements DB" in the code of my Data class. It is a trivial technicality given that DB does get implemented from implementing the interface that extends it. However, it's not worth risking me being failed over something silly that Oracle expects to be on in my code, regardless of what interfaces I have extended. In other words, I don't want to fail over one line of code. ;)

    Hopefully Roel or Robert will clarify if they took the exam super literal (as I am) or if they passed by just coding:


    It's nit-picky, but worth it if it saves me $200.

    Thanks.
    Matt
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5444
        
      13

    if your Data class passes the test aDataInstance instanceof DB, you have met the must requirement of implementing the interface
    Matt Pavlovich
    Ranch Hand

    Joined: Aug 14, 2010
    Posts: 98
    Perfect. Thank you.
     
    Consider Paul's rocket mass heater.
     
    subject: Adding Methods to the Interface