This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Instance of an abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instance of an abstract class" Watch "Instance of an abstract class" New topic
Author

Instance of an abstract class

MrKamal Joshi
Greenhorn

Joined: Jul 31, 2011
Posts: 24

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

Joined: Nov 04, 2008
Posts: 146

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


The important thing is not to stop questioning.Curiosity has its own reason for existing.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4370
    
    8

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 Marczak
Greenhorn

Joined: Aug 04, 2011
Posts: 6

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

Joined: Nov 04, 2008
Posts: 146

Hi,

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



Hope this will clear your confusion

Thanks
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3487
    
  13
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.


Joanne
Aniela Marczak
Greenhorn

Joined: Aug 04, 2011
Posts: 6

Yeap, it did, thanks

Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

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
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
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.


Ping & DNS - my free Android networking tools app
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

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


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

Hi Maneesh,

Absolutely right.

Thanks
 
 
subject: Instance of an abstract class