• Post Reply Bookmark Topic Watch Topic
  • New Topic

abstract class instance  RSS feed

 
Vijay Kumar
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clander is an abstract class and it can not be instantiated.
then why does this statement work..

Calendar rightNow = Calendar.getInstance();


thanks
Vijay
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you try this...



That should answer your question.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use the code above, you will see that Calendar.getInstance() does not create an instance of Calendar. As you said, this is not allowed. Instead, it instantiates some subclass and returns a Calendar reference to the subclass object.

Layne
[ March 10, 2005: Message edited by: Layne Lund ]
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Layne Lund:
If you use the code above, you will see that Calendar.getInstance() does not create an instance of Calendar. As you said, this is not allowed. Instead, it instantiates some subclass and returns a Calendar reference to the subclass object.

Layne

[ March 10, 2005: Message edited by: Layne Lund ]


Well, go ahead and just tell them what will happen! :-) I usually like to play with things like this in code myself. I find that I learn more that way.
 
Surasak Leenapongpanit
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If its subclass GregorianCalendar can be used instread of Calender.getInstance(), why does Calendar class provide getInstance()?
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't have to return a GregorianCalendar, just an object that extends Calendar. If you look at the source code, getInstance() calls this method ...



So, as you can see, it can sometimes return a BuddhistCalendar.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by James Carman:


Well, go ahead and just tell them what will happen! :-) I usually like to play with things like this in code myself. I find that I learn more that way.

I'm not the OP, and I have a decent idea of what will happen. But I don't know enough to give an exact answer. As I hinted at in my reply, your code will print out the name of the particular subclass that was returned. However, my post was more intended for the OP. I was assuming that he/she would try your code and I was trying to give a preemptive explanation of the behavior he/she would see.

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!