interface - Object class, java API -
when implement queue interface, requires me implement abstract methods within interface. however, methods has input variable of type object. refer to, , possible change string type or other primitive type in java? because, example, in abstract method:
import java.util.collection; import java.util.iterator; import java.util.queue; public class myqueue implements queue { @override public boolean add(object e) { throw new unsupportedoperationexception("not supported yet."); /*inserts specified element queue if possible without violating capacity restrictions, returning true upon success , throwing illegalstateexception if no space available. */ } }
when start implement method make own queue class input of string type instead of object type, tells me implement original abstract method again! can see in following snippest:
@override public boolean add(string name) { boolean result = true; if (rear == maxsize - 1) { result = false; throw new illegalstateexception("there no enough space."); } else { names[++rear] = name; front = 0; } return result; }
queue<e>
generic interface. means has type parameter e
.
if write
class myqueue<e> implements queue<e> { ... }
then required override add(e e)
, not add(object e)
. should write.
if write
class mystringqueue implements queue<string> { ... }
then required override add(string e)
. should write if implementation works string
s.
if write
class myqueue implements queue { ... }
then forced override add(object e)
. should not use raw type queue
without type parameter.
the easiest way implement queue
make class extend abstractqueue<e>
because of hard work done you.
Comments
Post a Comment