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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Implementing two interface methods with same signature

Status
Not open for further replies.

jjoensuu

MIS
Oct 22, 2001
45
0
0
US
Dear ladies and gents...

I have a newbie question about Java interfaces. I have not implemented any interfaces yet, so I do not know if what I am describing here could ever happen in "real life"...

Let's say I create a class that implements two interfaces that both have a method with the same signature. Furthermore, even though the methods have the same signature, they are supposed to carry out different functions. As a rather lousy hypothetical example:


interface VacationSpots {
//return true if location is a resort
boolean resort();
}

interface SortRooms {
//return true once rooms are sorted
boolean resort();
}

public class Resort implements VacationSpots, SortRooms {
//other code
boolean isResort;
//other code (constructor etc)
public boolean resort(){
return isResort;
}
//other code
}

QUESTION: In this sort of a case, which "resort" method is actually implemented?

TIA

JJ
 
In a class, you can just implement one method for each signature.

In that case, an unique method will apply for both cases.

Cheers,
Dian
 
The class defines the method, not the interface - so while the syntax/scenario is a bit odd, it is valid.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Just remember that, conceptually, the interface is specifying a 'contract'(i.e assumed behaviour) between the classes which implement it, and the classes which use instances of it.

If you have a class which implements two interfaces, both of which specifiy the same method, then that method would have to satisfy two possibly distinct contracts. Any consumer class using your method with respect to one interface (i.e one contract) would get behaviour put in place to satisfy the second contract too. This would probably be a bad side-effect.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top