• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Instance of an abstract class

 
Greenhorn
Posts: 24
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 148
Hibernate Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Greenhorn
Posts: 6
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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



Hope this will clear your confusion

Thanks
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeap, it did, thanks

 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Maneesh,

Absolutely right.

Thanks
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic