Java Reference
In-Depth Information
3. An interface may extend any number of (super-) interfaces.
4. A class may implement as many interfaces as it likes. Notice that this will require our
changing the parser's classDeclaration() method so that it parses an (optional)
implements-clause and stores a (possibly empty) list of interfaces.
5. Every field declaration in an interface is implicitly public , static , and final , but
any of these three modifiers may be present even if unnecessary. Every field must be
initialized. The initializing expression need not be constant and may reference other
fields appearing textually before the reference.
6. The initializing expression in an interface's eld declaration must involve only con-
stants or other static fields.
7. There are rules about the inheritance of fields; see the language specification.
8. Every method declaration in an interface is implicitly public and abstract but either
of these modifiers may be present in the declaration. Because the methods are abstract,
its body is always a semicolon.
9. An interface's method can never be declared static or final , but class methods
implementing them can be final .
10. There are additional rules governing method inheritance, overriding, and overloading
abstract methods in interfaces; see the language specification.
11. Any class implementing an interface (or a list of interfaces) must implement all of the
methods of the interface(s).
In code generation, there are several facts to consider:
1. In the JVM, an interface is a kind of class. So, just as for a class, one uses CLEmitter 's
addClass() method for introducing an interface.
2. The list of modifiers for the addClass() 's formal parameter accessFlags must include
both interface and abstract . The superClass argument should be the JVM name
for java.lang.Object . Of course, the argument passed for superInterfaces should
be the (possibly empty) list of the super-interfaces (from the extends-clause).
3. The list of modifiers of any field defined by addField() must include public , static ,
and final .
4. The list of modifiers of any method defined by addMethod() must include public and
abstract .
5. In invoking a method on an interface, that is, an object declared to be of a type defined
by an interface, one uses the invokeinterface instruction. Although less ecient
than invokevirtual , invokeinterface deals with the possibility of interfaces that
inherited multiple interfaces, and multiple classes implementing the same interface.
Otherwise, use JClassDeclaration as a guide to, and so a starting point for, imple-
menting JInterfaceDeclaration .
Exercise 5.2. Study the Java Language Specification [Gosling et al., 2005] and determine
what must be done to incorporate the final modifier into j--. Add the final modifier to j--,
adding it to your compiler and testing it thoroughly.
 
Search WWH ::




Custom Search