| Author |
B&S 2.1.2 Interface
|
Marcel Svacina
Greenhorn
Joined: Jan 07, 2009
Posts: 14
|
|
Hi
Please, is it possible to add more methods to the DB interface? For example, I would like to add method to get all records from DB file.
Many thanks for all opinions.
|
 |
Roberto Perillo
Bartender
Joined: Dec 28, 2007
Posts: 2216
|
|
|
No. However, you can create your interface with the methods you want, extending the interface that was provided to you, and implement your interface.
|
Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
|
 |
Marcel Svacina
Greenhorn
Joined: Jan 07, 2009
Posts: 14
|
|
But in my assigement is written:
"Your data access class must be called "Data.java", must be in a package called "suncertify.db", and must implement the following interface"
So I think, that my class Data.java has to implement DB interface.
|
 |
Roberto Perillo
Bartender
Joined: Dec 28, 2007
Posts: 2216
|
|
If you do what I suggested above, your Data class will implement the interface provided by Sun, right?
|
 |
Marcel Svacina
Greenhorn
Joined: Jan 07, 2009
Posts: 14
|
|
Sorry, there was a problem with my English :-)
If I am not understanding right correct me. You say, that I may add extends declaration to DB interface, but I can not add method to the DB interface? Why?
Thanks
|
 |
Roberto Perillo
Bartender
Joined: Dec 28, 2007
Posts: 2216
|
|
Hey, champ.
You cannot alter the interface that was provided to you. However, you can create your own interface with the methods you want, and extending the interface that was provided to you. For instance:
If you do this, then your Data class will implement the interface that was provided by Sun, and you will also be able to use the methods of your interface when using the Data object.
|
 |
Marcel Svacina
Greenhorn
Joined: Jan 07, 2009
Posts: 14
|
|
Thanks for your patience :-)
Sorry, it is little bit confusion for me. Are you sure, that in this way, when Data implements MyInterface, that it means that it implements both interface? MyInterface and SunInterface? For me, in Czech, it sounds that Data class implements all methods from MyInterface and all methods from SunInterface, but SunInterface's methods are inherit by MyInterface.
So if I will write
I comprehend, that data implements SunI and MyI but if I will write
so in this way my Data class implements only MyI which extends SunI, but still implements only MyI
|
 |
Anne Crace
Ranch Hand
Joined: Aug 29, 2005
Posts: 223
|
|
|
You got it! That's exactly what he means. Since Data is a concrete class, you can also add methods to it. I added a helper method to my Data class for adding whitespace to the ends of Strings to help with adding new records to the file (making sure they are long enough).
|
SCJP, SCJD
|
 |
Marcel Svacina
Greenhorn
Joined: Jan 07, 2009
Posts: 14
|
|
Can you assure me, that when I write code above, it means, that Data implements MyI and SunI? And that it is not differ with must "Data class must implement SunI" and I will not fail? In my born language it little bit sounds like that Data implements only MyI, where MyI iherit methods from SunI. It is misquided and hard to explaint with my English :-)
|
 |
Anne Crace
Ranch Hand
Joined: Aug 29, 2005
Posts: 223
|
|
|
If it makes you feel more comfortable, just have Data implement BOTH interfaces. Remember, you can implement as many interfaces as you want. I was doing that until I read Roberto's post, and just had it implement the interface that I extended. Eclipse wouldn't let me do it if it was wrong. I'm sure he's right about that, it's inheritance at work.
|
 |
Marcel Svacina
Greenhorn
Joined: Jan 07, 2009
Posts: 14
|
|
If you programm against interface, it is as for me bad way to write
When you write
you are not able to use methods from MyI without casting to MyI, so as I said, that is not good solution.
Roberto's post is absolutely right, but my still not answered question is, if this means
1) that Data implements MyI and SunI
2) or Data implements MyI and inhereted methods from SunI. This case does not realize must implements DB interface requirement
This is only 'play on word' for me. I need to be sure, that i will not fail on this 'play on words', if you understand me :-)
|
 |
Roberto Perillo
Bartender
Joined: Dec 28, 2007
Posts: 2216
|
|
Hey, partner.
If you do what I suggested, your Data class will have to implement all methods from Sun's interface + all methods of your interface.
The only problem with not extending Sun's interface and having your Data class implement the interfaces separately, like in:
public class Data implements YourInterface, SunsInterface
instead of
public class Data implements YourInterface
is that you won't be able to do this:
YourInterface data = new Data();
data.methodFromSunsInterface();
Bottom line: you can create your interface and extend Sun's interface without any problem. I did it and didn't fail!
|
 |
Marcel Svacina
Greenhorn
Joined: Jan 07, 2009
Posts: 14
|
|
That is what I want to hear :-)
I did it and I did not fail
Thanks
|
 |
 |
|
|
subject: B&S 2.1.2 Interface
|
|
|