Java Reference
In-Depth Information
PITFALL: (continued)
Suppose a class defi nition begins with
public class MyClass
implements Interface1, Interface2
{ ...
Clearly this has to be, and is, illegal. The defi ned constant ANSWER cannot be simul-
taneously 42 and 0 . 2
Even two method headings can be inconsistent. For example, consider the following
two interfaces:
inconsistent
method
headings
public interface InterfaceA
{
public int getStuff();
}
public interface InterfaceB
{
public String getStuff();
}
Suppose a class defi nition begins with
public class YourClass
implements InterfaceA, InterfaceB
{ ...
Clearly this has to be, and is, illegal. The method getStuff in the class YourClass
cannot be simultaneously a method that returns an int and a method that returns a
value of type String . (Remember that you cannot overload a method based on the
type returned; so, overloading cannot be used to get around this problem.)
Self-Test Exercises
8. Will the following program compile? If it does compile, will it run? Interface1
and Interface2 were defi ned in the previous subsection.
public class MyClass
implements Interface1, Interface2
{
public static void main(String[] args)
{
System.out.println(ANSWER);
}
}
(continued)
2 If the class never uses the constant ANSWER , then there is no inconsistency and the class will compile
and run with no error messages.
 
Search WWH ::




Custom Search