Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Sun made Calendar class as a abstract class

 
jitendra vishnoi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know why Sun make Calendar(or DateFormat or NumberFormat etc.) class as a abstract class. This class can also be as a normal class and its methods can also be have some implementation. We can also get instance of Calendar class by Calendar.getInstance() without extending the Calendar class.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calendar is abstract because there is no global calendar system. Most use the Gregorian calendar; that's why it has a matching concrete class GregorianCalendar. There are at least two other active calendars - the Buddhist calendar and the Japanese imperial calendar. Those too have matching (non-public) classes in Java.

DateFormat and NumberFormat are abstract because, well, that's how it's designed. They are meant to be used as the root of entire formatter hierarchies*. They provide no direct implementation; instead that's up to subclasses like SimpleDateFormat (the most often used DateFormat subclass) and DecimalFormat (the most often used NumberFormat subclass). But because DateFormat and NumberFormat are abstract it's easy to write and use a different one.


* Actually sub hierarchies, with java.text.Format being the root of the entire formatter hierarchy.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic