java - Bound Mismatch on new object of generic class -
i have error occuring program assignment. in it, have create own generic
public class linkedlist<e extends comparable<t>> implements list<e> {
the implemented interface is:
public interface list<e extends comparable<t>> { }
now, whenever try create new object of type linkedlist follows:
linkedlist<termin> k = new linkedlist<termin>();
eclipse gives me following error:
- bound mismatch: type termin not valid substitute bounded parameter > of type
linkedlist
the class declaration of class termin follows:
public class termin implements comparable<t> { }
in case need constructor , variables of linkedlist object:
private e item; private linkedlist<e> next; //constructor public linkedlist() { item = null; next = null; }
with little google magic, found out there once bug involving generics in eclipse gave same error no reason.
i suppose of declarations aren't entirely correct.
the way code written, cannot compile number of reasons, including lack of specification of t
type, not explicitly declared.
one solution remove t
, replace known java
type (e.g., object
).
a more generic solution include t
, means 2 generic types have used.
for latter case, code like:
// linkedlist class public class linkedlist<t, e extends comparable<t>> implements list<t, e> { private e item; private linkedlist<t, e> next; // constructor public linkedlist() { item = null; next = null; } public static void main(string[] args) { // example statement, t = long.class , e = string.class linkedlist<long, termin<string>> k = new linkedlist<long, termin<string>>(); } }
and
// list interface public interface list<t, e extends comparable<t>> { }
and
// termin class public class termin<t> implements comparable<t> { @override public int compareto(t o) { return 0; // actual comparison needs implemented } }
Comments
Post a Comment