General expands and implements

I do not understand why Companycompiled. I thought he was tested on extends, but not on implements?

public interface Employee

public class HourlyEmployee implements Employee

public class Company<T extends Employee>

Company<HourlyEmployee> company = new Company<>();
+4
source share
2 answers

The keyword extendsin Generics has slightly different semantics than the general keyword extends.

When used extendsin the context of Generics, for example T extends Something, this means that it Tmust be a type that either implements an interface Something(in cases where it Somethingis an interface), or is a subclass Something(in case it Somethingis a class).

, , implements Generics, .

, :

<T extends SomeClass implements Serializable & Observable>

:

<T extends SomeClass & Serializable & Observable>

implements. T , T, , .

. , - T.

+8

T extends Employee .

public class Company<T implements Employee> .

, public class Company<T extends Employee> , T Company Employee.

+4

Source: https://habr.com/ru/post/1615698/


All Articles