my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusion in Calender Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confusion in Calender Class" Watch "Confusion in Calender Class" New topic
Author

Confusion in Calender Class

Malhar Me
Greenhorn

Joined: Jun 22, 2009
Posts: 27
Hello everyone,

I ahve one question. Calender class in java is abstract class. But if you want to use it then you are creating its instance using
Calendar rightNow = Calendar.getInstance();

Here is the few lines which are in help on Suns site:

A Calendar object can produce all the time field values needed to implement the date-time formatting for a particular language and calendar style (for example, Japanese-Gregorian, Japanese-Traditional). Calendar defines the range of values returned by certain fields, as well as their meaning. For example, the first month of the year has value MONTH == JANUARY for all calendars. Other values are defined by the concrete subclass, such as ERA and YEAR. See individual field documentation and subclass documentation for details.


The method details:

getInstance

public static Calendar getInstance()

Gets a calendar using the default time zone and locale. The Calendar returned is based on the current time in the default time zone with the default locale.

Returns:
a Calendar.

Here is my question:
(1) If the Calender class is abstract how it clould be possible to create a calender object???

Can anybody give me the exact explanation about this? How to create a object of the abstract class. or how the calender object create here???

Malhar
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Calendar class has 8 or so abstract methods. You can create an instance of an abstract class by either sub-classing it normally or using anonymous inner class. And as far as Calendar goes, it returns an instance of one of the concrete sub-class GregorianCalendar. I don't know if there is any exception to this...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
aabir sanyal
Greenhorn

Joined: Jun 22, 2009
Posts: 21
Ankit Garg wrote:Calendar class has 8 or so abstract methods. You can create an instance of an abstract class by either sub-classing it normally or using anonymous inner class. And as far as Calendar goes, it returns an instance of one of the concrete sub-class GregorianCalendar. I don't know if there is any exception to this...


Not satisfied with this explanation. Want a better one. Nice question by the way.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40

aabir sanyal wrote:Not satisfied with this explanation. Want a better one. Nice question by the way.


It would help if you explain what you are not satisfied with.... We can't elaborate, if we don't know what you don't understand.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40

Here is my question:
(1) If the Calender class is abstract how it clould be possible to create a calender object???


Keep in mind that subclasses of a superclass IS-A superclass type too. So, returning an instance of a subclass (concrete) of the calendar class IS returning a calendar object.

Henry
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

The Calendar.getInstance() method basically looks like this:

GregorianCalendar is a non-abstract subclass of Calendar.


SCJP 6 || SCWCD 5
aabir sanyal
Greenhorn

Joined: Jun 22, 2009
Posts: 21
Michael Angstadt wrote:The Calendar.getInstance() method basically looks like this:

GregorianCalendar is a non-abstract subclass of Calendar.


Perfect, thanks.
Malhar Me
Greenhorn

Joined: Jun 22, 2009
Posts: 27
Thanks for the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion in Calender Class
 
Similar Threads
Instance of an abstract class
Abstract classes can't be instantiated?
3 months
abstract method puzzler
Downcasting And Upcasting