Java Reference
In-Depth Information
10. One of the problems with the class structure used by Java and C
is
that field and method declarations (which are terse) are intermixed with
method implementations (which can be lengthy and detailed). As a
result, it can be hard to casually ”browse” a class definition.
As an alternative, assume we modify the structure of a class to sepa-
rate declarations and implementations. A class begins with class dec-
larations. These are variable and constant declarations (completely un-
changed) as well as method headers (without method bodies).
An ”implemented as” section follows, which contains the bodies of each
method declared in the class. Each method declared in the class must
have a body defined in this section, and no body may be defined unless
it has been previously declared. Here is a simple example of this revised
class structure:
class demo {
char skip = '\n';
int f();
void main();
implemented as
f:
{return 10;}
main:
{print("Ans =",f(),skip); }
}
What changes are needed in the semantic analysis of classes andmethods
to implement this new class structure?
11. Just as variables and fields may be initialized, some programming lan-
guages allow formal parameters in methods to be initialized. An initial-
ized parameter provides a default value .Inacallofamethod,ausermay
choose to not provide an explicit parameter value, choosing the default
instead. For example, given:
int power(int base, int expo = 2) {
/* compute base**expo */}
the calls power(100,2) and power(100) both compute the same value
(100 2 ).
What changes would be needed in the semantic analysis of method calls
to correctly handle initialized formal parameters? Be sure to consider
how overload resolution is a
ff
ected.
 
Search WWH ::




Custom Search