• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instance of an abstract class

 
MrKamal Joshi
Greenhorn
Posts: 24
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How the instance of an abstract class is created?As there exist a getInstance() method for an abstract class java.util.Calender.
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Although Calender class has a abstract method getInstance(), but when you say Calendar calc=Calendar.getInstance();

calc is referring to the class instance of class GregorianCalendar as "GregorianCalendar extends Calendar "

Thanks
 
Matthew Brown
Bartender
Posts: 4566
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you ever want to know the exact class that is returned (and usually you don't need to know - that's the point of polymorphism - but it might be interesting) then just print out the value of theObj.getClass().getName().
 
Aniela Kącka
Greenhorn
Posts: 6
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...

I'm not sure, if really calling the getInstance() static method of the Calendar class, you really make an instance of GregorianCalendar. However abstract class can never be directly instantiated, there is something called "annonymous inner type", which allows you to create a no-name subclass of the abstract class and an instance of this:


As you can see, you must then implement all the abstract methods from the abstract superclass (Calendar in this case).

I think the getInstance() method of Calendar class might work like that.
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If you look at the source of getInstance() will you see



Hope this will clear your confusion

Thanks
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijay Tidake wrote:If you look at the source of getInstance() will you see

And this is a perfect example of polymorphism in action. I don't know which version of the source code Vijay looked at but I looked at the 1.6_20 source code and it's createcalendar method looked like this

So, the code has been changed but anyone who uses the Calendar.getInstance method doesn't need to worry about it or make any changes to their source code to handle it.
 
Aniela Kącka
Greenhorn
Posts: 6
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeap, it did, thanks

 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joanne Neal,
I don't know which version of the source code Vijay looked


Im using jrockit-jdk1.6.0_20-R28.1.0-4.0.1

Thanks
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The crucial point is that "Calendar.getInstance()" does NOT return an instance of the Calendar class - rather it returns whatever the getInstance mathod creates. That can't be an instance of Calendar, because that is abstract - it must be an instance of a subclass of Calendar that is not abstract. Mathew mentioned how you can determine what actual class that is.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijay Tidake wrote:
Although Calender class has a abstract method getInstance(), but when you say Calendar calc=Calendar.getInstance();
calc is referring to the class instance of class GregorianCalendar as "GregorianCalendar extends Calendar "

Calendar#getInstance() is not abstract, it is static. Two different things. The Calendar class is abstract.

Vijay Tidake wrote:
calc is referring to the class instance of class GregorianCalendar as "GregorianCalendar extends Calendar "

Not always. As quoted from the API Doc
Gets a calendar using the default time zone and locale.

If you actually look at the comment in the code snippet you posted, it can also return the Buddhist and JapaneseImperialCalendar which are not Gregorian
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maneesh,

Absolutely right.

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic