It's not a secret anymore!*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Extending Sun interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Extending Sun interface" Watch "Extending Sun interface" New topic
Author

Extending Sun interface

Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hello Everybody,

I had the need to add a few public methods to my Data class that implements my own interface that
extends the interface in the assignment provided by Sun.
For example in my own interface I have a getAllRecords() method.

Do all the public methods (signature) present in my Data class that are not in the interface provided by Sun,
should be present in my own interface I am extending from Sun?

Thanks,

Carlos.


SCSA, OCA, SCJP 5.0, SCJD http://www.linkedin.com/in/carlosamorillo
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5209
    
  12

Hi Carlos,

In my humble opinion: yes, they should. And the reason is quiet obvious: best practice would be to program against an interface, so you can easily change the implementation with another implementation without braking your code. And if you don't add this methods to your interface then you can't invoke these methods. And if it's not necessary they are called from somewhere else, methods should of course not be public.



If I need to replace Data with another implementation I have to implement the contract (interface) and replace all Data occurences with the new implementation class. I will get compiler errors because I didn't know that I had to implement doSomething! And it is possible that the other implementation is developed by someone else (just implement the contract for a RDBMS for example) and then this class is passed to someone else who has to adjust the main program. He will get a lot of red crosses and will hunt you down and probably want to use your head for some




Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2264
    
    3

Roel De Nijs wrote:In my humble opinion: yes, they should. And the reason is quiet obvious: best practice would be to program against an interface, so you can easily change the implementation with another implementation without braking your code. And if you don't add this methods to your interface then you can't invoke these methods. And if it's not necessary they are called from somewhere else, methods should of course not be public.


100% agreed.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Roel and Roberto,


Just what I thought and suspected, just wanted to double check.
Thanks once again,



Carlos.
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi Roberto and Roel,


Going back to this subject again, I still have a few things to sort out:

How do you handle the getInstance() methods that are public static if you implement the Data class as a Singleton pattern?

Also, if you make your Data class to read dynamically all the values from the schema in the database file
such as numberOfFields without hardwiring and declaring a lot of constants, I was thinking about having
a method getNumberOfFields() declared as public static that could be called by my Room class that has conversion methods
public static String[] toStringArray(Room room) and public static Room toRoom(String[] record).
In my Room class I have:



I do understand Roel's point about having all the public methods that my class exports in my interface extending Sun interface.


Thanks,


Carlos.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5209
    
  12

Hi Carlos,

The static getInstance method can't be in the interface of course. If you use a dao factory (like I did) this is not really a problem, because each dao will be created through this factory. So if it changes to not being a singleton for example, the only place you have to change it will be in this factory.

I didn't used the numberOfFields in the Room class, because in the perfect application you would create for example an interface MetaData (having methods to return numberOfFields, data type of each column, size of each column,...) and you will have a DataMD (implements MetaData). And then you would make calls against the MetaData interface. But in my opinion that was just 1 bridge too far (you could even use this metadata to pass to the client for limiting text fields etc) and I explained in my choices.txt this could be an enhancement for future releases

Kind regards,
Roel
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2264
    
    3

Howdy, Carlos!

Also, if you make your Data class to read dynamically all the values from the schema in the database file
such as numberOfFields without hardwiring and declaring a lot of constants, I was thinking about having
a method getNumberOfFields() declared as public static that could be called by my Room class that has conversion methods
public static String[] toStringArray(Room room) and public static Room toRoom(String[] record).


Hum... so you would call it from your Room class, right? I like the idea of having at least this intelligence (toRoom() and toStringArray() methods) in the Room class. But, why do you want to call the getNumberOfFields() method from there? To perform some validation?
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi Roberto,

In Room toRoom(String[] record) yes, I do check the length of the String[] to be converted to see if it's equal to
numberOfFields and if it isn't I throw an IllegalArgumentException
and in String[] toStringArray(Room room) I do instantiate a String[numberOfFields], that later I build and I will return.

Hi Roel,

I guess in your Room class you just solved this by declaring a


?


Thanks,


Carlos.

Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2264
    
    3

Howdy, Carlos!

You know, last monday, in the master program, my professor was talking about responsibilities, which is what the class knows and does. The class knows how many fields it has, so you can perform this validation based on the number of fields the class has without having to call the getNumberOfFields() from the Data class.

But still champion, if you feel confident that the getNumberOfFields() method is really needed, go for it!
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi Roberto,

So you think it isn't necessary?
Shall I just have a constant instead?

Thanks,

Carlos.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2264
    
    3

Hey, champion!

Well, I'd say that I constant would save some computing... the class already knows how many fields it has, so you can define this value in a constant and use it when performing the validations (the validation will be done against the class, not the database schema).
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5209
    
  12

Carlos Morillo wrote:I guess in your Room class you just solved this by declaring a static private final NUMBER_OF_FIELDS= 7;

No Carlos, I didn't had to solve anything, because I just created an anonymous array. A bit sneaky, I know

Kind regards,
Roel
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi Roberto,

I hate to drill down so much on this, but you guys are making me steer
towards just defining a constant and also looking at it in terms of decoupling
the Room class from the Data class so Room doesn't have to rely on any knowledge
of the Data class.
I think this way it will look way much cleaner.

because I just created an anonymous array.


Roel,

Well at some point you have to instantiate the String[] and specify the size with 7 elements.

Anyway, I am declaring the constant in Room and getting rid of static public int getNumberOfFields() in my Data class.

Thanks,

Carlos.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5209
    
  12

Carlos Morillo wrote:Well at some point you have to instantiate the String[] and specify the size with 7 elements.
That's true, but that happens only in the create method of the Data class (and in this class you have knowledge about the database schema, so no constant required). In the method to convert my room object to a String[] I just have this code:
Kind regards,
Roel
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi Roel,

I still think if you are going to validate the String[] length in a method like public static Room toRoom(String[] record)
you would still need a constant.

The static getInstance method can't be in the interface of course. If you use a dao factory (like I did) this is not really a problem


Is it necessary to define an interface to be implemented by the DAO Factory object?

Thanks,

Carlos.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5209
    
  12

Hi Carlos,

I still think if you are going to validate the String[] length in a method like public static Room toRoom(String[] record)
you would still need a constant.
That's true, but I didn't do a check. Is one of the very few methods which parameters are not validated.

Is it necessary to define an interface to be implemented by the DAO Factory object?
I don't know exactly what you mean, but I found this link very helpful.

Kind regards,
Roel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extending Sun interface