Extend java.util.calendar class -
i want extend java.util.calendar class in order add functionality:
import java.util.calendar; abstract class extendedcalendar extends calendar { public static extendedcalendar getinstance() { calendar c = calendar.getinstance(); return (extendedcalendar)c; } } i exception, because calendar.getinstance returns gregoriancalendar:
java.lang.classcastexception: java.util.gregoriancalendar cannot cast extendedcalendar but how possible cast of gregoriancalendar calendar working?
calendar calendar = (calendar)c; how can solve problem?
calendar.getinstance() static factory method event not allow customizations or injections. returns instance of concrete calendar. in cases gregoriancalendar. can buddhistcalendar or japaneseimperialcalendar depending on locale.
if want create instance of calendar direct call of constructor: new extendedcalendar().
btw imho not have extend gregoriancalendar. extending functionality not limited extending class. create own class uses other calendar in calculations instead of extending it.
Comments
Post a Comment