java - Whether to use generics or not? -
i have following hierarchy of classes
public abstract class myabstractclass { } public abstract class abstractclassa extends myabstractclass { } public class abstractclassb extends myabstractclass { } public class classa extends abstractclassa { } public class classb extends abstractclassa { }
i have converter should accept instance of class
extending abstractclassa
, should return instance
of class
extending abstractclassb
. have 2 versions of conversion class 1 using generic , other 1 not. both works fins. correct way of specifying signature of converter?
public class convertertwo { public abstractclassb convert(abstractclassa a) { } } public class converterone { public <a extends abstractclassa, b extends abstractclassb> b convert(a a) { } }
generics checked @ compile time , replaced proper types during compilation.
convertertwo
code converted converterone
kind of code during compilation , both equivalent.
generics useful when write api not know kind of objects consumer of api's going pass. in case, if plan write generic adapter.
classic example of genrics : collection interface , classes implement it.
Comments
Post a Comment