Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-09-2008, 01:14 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Implementing an interface
Hello!

I am defining an interface.

Code:
interface Parser{ void parseText(String buffer); void parseInt(String buffer); void parseFloat(String buffer); }
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.

How this can be done?

Thanks a lot for yout time.
- Cheers
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-09-2008, 02:35 PM
jelly's Avatar
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
jelly is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
An interface extending an interface(II) JavaForums Java Blogs 0 03-12-2008 04:00 PM
Implementing ActionListener interface JavaForums Java Blogs 0 12-19-2007 10:54 PM
Creating a Thread (implementing Java Runnable Interface) JavaForums Java Blogs 0 12-19-2007 10:31 AM
Implementing Serializable interface javaplus Advanced Java 4 12-18-2007 01:29 PM
Implementing Interface mew New To Java 2 12-06-2007 10:09 PM


All times are GMT +3. The time now is 04:35 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org