Extending base class containing abstract generic method in java -
i given base of operations class this:
class mybase{ public abstract <t> pupil assign (t object); }
i extending below:
class myclass extends mybase{ @override public abstract <t> pupil assign (t object){ pupil stew = new student(); ...... } }
my intended utilize is: passed in object should of type teacher. within method want create new pupil values invoking functions on teacher. (for example, stew.setteachername = teacher.getname();) , homecoming pupil caller.
questions: a) how intended behavior? b) whats difference between declaring mybase current way vs. doing mybase ? c) interested in knowing solution in case can alter base of operations class , cannot alter base of operations class.
thanks in advance help. also, if there useful resources/tutorials can point me great. again.
if cannot modify base of operations class, there isn't way involves generics. there 2 things do. one, check run-time type of object , 1 thing if teacher, if not.
public class myclass extends mybase { @override public <t> pupil assign(t object) { pupil student = new student(); if (object instanceof teacher) { teacher teacher = (teacher) object; student.setteachername(teacher.getname()); } else { student.setteachername("unknown"); } homecoming student; } }
two, create 2 separate assign
methods in myclass, 1 overrides generic method in base of operations class, , non-generic method takes teacher parameter.
public class myclass extends mybase { @override public <t> pupil assign(t object) { homecoming new student(); } public pupil assign(teacher teacher) { pupil student = new student(); student.setteachername(teacher.getname()); homecoming student; } }
when method called, if compile-time type teacher, compiler take more specific overload of method:
public class main { /** * gets teacher calls object. * * @return object happens teacher */ public static object getteacher() { homecoming new teacher(); } public static void main(string[] args) { /** teacher, , declared such */ teacher teacher = new teacher(); /** * object, happens * teacher (but compiler can't see that) */ object = getteacher(); myclass mc = new myclass(); pupil withteacher = mc.assign(teacher); pupil withoutteacher = mc.assign(something); } }
however, much prefer 1 of solutions modifies mybase.
edited add: declare second, more specific assign
method generic method, so:
public <t extends teacher> pupil assign(t object) { pupil student = new student(); student.setteachername(object.getname()); homecoming student; }
but same sec illustration above. method not override method in base of operations class, still relies on compiler picking more specific overload @ compile time, , erasure of method signature same non-generic method in sec example:
public pupil assign(teacher)
it looks bit more override of method base of operations class because it's got generic type variable , parameter named object
instead of teacher
. seek sticking @override
annotation on , compiler tell not override or implement method supertype.
and still have <t>
generic method hanging around doesn't set teacher name, , if ever have teacher object not declared such, compiler take wrong overload , you'll scratching head wondering why, if object teacher, teacher name didn't set on student.
java generics java-7 abstract
No comments:
Post a Comment