Results 1 to 2 of 2
Thread: Implementing an interface
- 01-09-2008, 12:14 PM #1
Senior Member
- Join Date
- Nov 2007
- Posts
- 111
- Rep Power
- 0
Implementing an interface
Hello!
I am defining an interface.
Now the classes implementing this interface have to implement these methods. I want to keep the implementation of parseText(...) method as optional. The class implementing this interface have to implement parseInt(...) and parseFloat(...) but parseText(...) should be optional.Java Code:interface Parser{ void parseText(String buffer); void parseInt(String buffer); void parseFloat(String buffer); }
How this can be done?
Thanks a lot for yout time.
- Cheers
- 01-09-2008, 01:35 PM #2
An interface is best thought of as a contract, if you implement the interface then you need to implement the methods. You _ could_ simply implement parseText as
parseText(...) {}
but that would confuse the calling code that would have expected some process to have taken place. If you do not want all classes to implement parseText then perhaps two interfaces
interface Parser{
void parseInt(String buffer);
void parseFloat(String buffer);
}
interface SuperParser extends Parser {
void parseText(String buffer);
}
The second interface is now a subinterface of the first and adds additional capability. A user could check what capabililty is available via the instanceOf keyword
if ( someObjRef instanceOf SuperParser) {
someObjRef.parseText(...);
}-- Hope that helps
Similar Threads
-
Implementing Interface
By mew in forum New To JavaReplies: 4Last Post: 02-16-2010, 03:33 PM -
Implementing Serializable interface
By javaplus in forum Advanced JavaReplies: 4Last Post: 12-18-2007, 12:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks