> -----Original Message----- > From: Thomas Hicks [mailto:[EMAIL PROTECTED] > > 1) Is it true that an Interface cannot specify Constructor behavior?
Yes. In a quick scan of the JLS I could not find the restriction stated explicitly, but it is implied by the formal grammar for an interface in section 9.1.3 (http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html#236431). An interface may declare constants, abstract methods, classes and interfaces. Constructors are not mentioned. > 2) Why not? An interface doesn't need a constructor because it does not declare something that can be constructed. It only defines a set of behaviors that some set of classes may agree to provide. The details of how they provide that behavior is none of the interface's business. You can't write Country c = new Country("USA", "United States"); because Country can't be instantiated in its own right; you have to instantiate a class that implements Country. And it's up to that class to decide what its constructor looks like as well as how it knows what Country it's associated with. At least in part, this restriction flows from the decision made by Java's designers to avoid C++-style multiple inheritance. We have multiple inheritance of interface but only single inheritance of implementation. If you put a constructor into an interface, you are specifying an implementation detail: the information required to create an instance. Consider your 'Country' interface. Methods that return the country name and ISO code are perfectly reasonable since those are things any class implementing Country should be able to provide. But if you specify a constructor as well then your interface is meddling in the definition of where the information comes from, and that is an implementation detail that properly belongs to the class implementing Country. You may be thinking that any class implementing Country will need to be told what country a given instance represents. But suppose I wrote the following class: public class PointOnEarth implements Country { public PointOnEarth(float latitude, float longitude) { ... } public String getCode() { ... } public String getName() { ... } } Assuming the presence of a really good GIS :-), this class implements the Country interface without needing to have the country specified either at construction or any other time. So what would we do with the Country constructor if there was one? All that Country should be concerned with is that an instance of it can identify what country it represents, and not how it knows that. > p.s. What I have in mind is something like the following: Depending on what you really want to accomplish, an abstract base class may be more appropriate than an interface. That approach does allow you to specify construction-time requirements, since any subclass constructor is required to invoke one of its superclass's constructors. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
