Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't a Java Generic implement an Interface? [duplicate]

Is there a logical reason why E cannot implement my interface HasName?

public class SinglyLinkedList<E extends HasName> {
    // stuff...
}
like image 557
Wang-Zhao-Liu Q Avatar asked Sep 16 '13 00:09

Wang-Zhao-Liu Q


People also ask

Can we implement an interface twice?

In interfaces, a class can implement more than one interface which can't be done through extends keyword.

Can a generic implement an interface?

Only generic classes can implement generic interfaces. Normal classes can't implement generic interfaces.

What happens if two interfaces have the same method in Java?

A class implementation of a method takes precedence over a default method. So, if the class already has the same method as an Interface, then the default method from the implemented Interface does not take effect. However, if two interfaces implement the same default method, then there is a conflict.

What happens when two interfaces are created with same name?

If both interfaces have a method of exactly the same name and signature, the implementing class can implement both interface methods with a single concrete method.


1 Answers

The extends keyword applies to interfaces too. That is:

public class SinglyLinkedList<E extends HasName> {

Means that E must be a type that extends a class, or implements an interface, called HasName.

It is not possible to code E implements HasName - that is implied by E extends HasName.

like image 176
Bohemian Avatar answered Oct 27 '22 00:10

Bohemian