Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interface prob

Status
Not open for further replies.

db2sachin

Programmer
Jun 9, 2003
24
0
0
AU
I have one interface say 'I' with say 10 methods. I want to use only 5 methods from it in one of my class 'A', some other class 'B' may use rest of the methods or may be all of them. But for class 'A' I want only 5 methods. How can I achieve that ? Coz if I implement interface 'I' in class 'A' I'll have to implement all the 10 methods.
Thanx in adavance
 
The correct way to do it is either:
a) split up the interface into two interfaces and a marker interface
or
b) implement the methods and just don't do anything apart from throwing an UnsupportedOperationException on the methods you're not planning to support.

 
db2sachin, I would say your application (your interface) is not well designed. Why do you want to use an interface anyway in this situation? Follow jwenting's advice. And/Or depending on what you are trying to accomplish you could also make a (abstract) default implementation.
 
I would add another option to jwenting's suggestion:

c) like option a) you will break up the interface into multiple interfaces but instead of have a "marker interface" that two separate interfaces extend, you will have the parent interface contain the 5 methods for class "A". Then have a second interface extend the first interface and add the additional methods for class "B".

I agree with hologram though that this design sounds a little strange for use with interfaces.
 
I think everybody has savvy ideas. But think about the implication on future software changes. In short, if you think that you WILL DEFINITELY use those 5 methods and NONE OTHER, then go ahead and split them.

But for future portability, it may not be a bad idea to leave all those ten methods in one interface and leave blank implementations for each in your classs. This way, if you need to implement the methods for the other 1 to 5 methods, then the method signatures are already there. Without changing the codes at the class level, i.e. adding "implements" keyword to make use of the new interface, you can "quietly" provide the bodies for these existing/empty methods.



~za~
You can't bring back a dead thread!
 
You can create an abtract class implementing the interface,and define the methods in the abstract class; further classes can then extend this abstract class, only overriding the desired methods in each individual child classes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top