java - Constructor problems with inheritance and generics -
i having hard time understanding generics inheritance. error getting is:
stage.java:66: error: constructor stage in class stage<t> cannot applied given types; { ^ required: arraylist<t>,double,arraylist<t> found: no arguments reason: actual , formal argument lists differ in length t type-variable: t extends object declared in class stage
what have subclass called stage0 (an inline class of stage) inherits stage. stage0 have same functionality bar - stage0 @override method parent class. here line 63 of stage class (which beginning of stage0)
class stage0 extends stage<t> { stage0(arraylist<t> inq, double inputtime, arraylist<t> outq) { inputqueue = inq; takestime = inputtime; outputqueue = outq; } @override public boolean isstarving(double time) { return false; } }
what source of error?
cheers.
public class stage<t> // equivalent 'storage' { private t holditem; private boolean blocked; private double takestime, timetaken, blockedtime, starvetime; private arraylist<t> inputqueue, outputqueue; public stage(arraylist<t> inq, double inputtime, arraylist<t> outq) { inputqueue = inq; takestime = inputtime; outputqueue = outq; }
because haven't specified call super
in stage0
constructor code, compiler inserts you, though had typed this:
stage0(arraylist<t> inq, double inputtime, arraylist<t> outq) { super(); // <====================== inputqueue = inq; takestime = inputtime; outputqueue = outq; }
from error message, appears stage
has no matching constructor. error says:
constructor stage in class stage cannot applied given types...
required: arraylist,double,arraylist
found: no arguments
that is, you're trying call super()
when best matching constructor can find super(arraylist<t>,double,arraylist<t>)
.
the solution use super
explicitly, providing necessary arguments. in case, again based on error message, be:
stage0(arraylist<t> inq, double inputtime, arraylist<t> outq) { super(inq, inputtime, outq); // <====================== inputqueue = inq; takestime = inputtime; outputqueue = outq; }
Comments
Post a Comment