Java Reference
In-Depth Information
// You can tune a piano, but can you tuna fish?
public int getNumberOfScales() { return 91; }
}
the method getNumberOfScales in class Tuna has a name, signature, and return type that
matches the method declared in interface Fish and also matches the method declared
in interface Piano ; it is considered to implement both.
On the other hand, in a situation such as this:
Click here to view code image
interface Fish { int getNumberOfScales(); }
interface StringBass { double getNumberOfScales(); }
class Bass implements Fish, StringBass {
// This declaration cannot be correct,
// no matter what type is used.
public ?? getNumberOfScales() { return 91; }
}
it is impossible to declare a method named getNumberOfScales whose signature and re-
turn type are compatible with those of both the methods declared in interface Fish and
in interface StringBass , because a class cannot have multiple methods with the same
signature and different primitive return types (§ 8.4 ) . Therefore, it is impossible for a
single class to implement both interface Fish and interface StringBass 8.4.8 ).
A class may not at the same time be a subtype of two interface types which are different
invocations of the same generic interface (§ 9.1.2 ) , or a subtype of an invocation of a gen-
eric interface and a raw type naming that same generic interface, or a compile-time error
occurs.
This requirement was introduced in order to support translation by type erasure (§ 4.6 ) .
Example 8.1.5-4. Illegal Multiple Inheritance of an Interface
interface I<T> {}
class B implements I<Integer> {}
class C extends B implements I<String> {}
8.1.6. Class Body and Member Declarations
A class body may contain declarations of members of the class, that is, fields (§ 8.3 ) , meth-
ods (§ 8.4 ), classes (§ 8.5 ) , and interfaces (§ 8.5 ) .
A class body may also contain instance initializers (§ 8.6 ), static initializers (§ 8.7 ) , and de-
clarations of constructors (§ 8.8 ) for the class.
Search WWH ::




Custom Search