inheritance - Class equivalence in java -
a small hiccup led me basic question. intend check class equivalence in following case (academic curiosity - please not direct me instanceof or isinstance):
public class staticchild extends staticparent{ .... public static void main(string[] args){ staticchild thisobj=new staticchild(); system.out.println(thisobj.getclass()==staticparent.class); } } this gives compilation error:
error: incomparable types: class<cap#1> , class<staticparent> system.out.println(thisobj.getclass()==staticparent.class); cap#1 fresh type-variable: cap#1 extends staticchild capture of ? extends staticchild - what mean?
- what difference between type , class(specifically context - type , class of staticchild)?
- how rid of compilation error?
since thisobj declared of type staticchild, expression:
thisobj.getclass() returns object of type class<? extends staticchild>. trying compare object of type class<staticparent> compiler cannot match generic type parameters. knows staticparent cannot possibly extend staticchild there's nothing can assign ? in class<? extends staticchild>.
there several ways rid of error. instance, declare thisobj of type object (or of type staticparent).
note, however, test fail.
Comments
Post a Comment