Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
java_language_specification_7.pdf
Скачиваний:
13
Добавлен:
21.03.2016
Размер:
3.11 Mб
Скачать

CLASSES

Class Body and Member Declarations

8.1.6

In the following program:

interface Fish { int getNumberOfScales(); } interface Piano { int getNumberOfScales(); } class Tuna implements Fish, Piano {

// 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:

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 return 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 generic 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), methods (§8.4), classes (§8.5), and interfaces (§8.5).

195

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]