Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The Calendar abstract Class

 
Hicham bucarri
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the K&B SCJP certification Book and exactly in chapter6 (Dates, Numbers, and Currency (Exam Objective 3.4))
the author says that "In order to create a Calendar instance, you have to use one of the overloaded getInstance() static factory methods:"
but, the Calendar Class is an abstact one, and you cannot create an instance of an abstract class what ever the method you use!!

can anybody explain the author's point of view?
 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its like doing

List ls = new ArrayList();

List is an interface.
 
Rafael Souza
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually when you call Calendar.getInstance(); what happens inside getInstance is that an object of a concrete class is created. Normally it�s an instance of java.util.GregorianCalendar, which is concrete.

Think like this: among other stuff, the method Calendar.getInstance() does something like return new GregorianCalendar(); So it doesn't try to instantiate Calendar (which is abstract), but GregorianCalendar (which is concrete).

Try to run the code that follows and you will notice that the result is: java.util.GregorianCalendar

 
Hicham bucarri
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you victor!
 
Hicham bucarri
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your version Rafael is better!
Thanks again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic