• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Abstract classes can't be instantiated?

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that abstract classes can't be instantiated. So, how come "public abstract class Calendar" has a constructor and get instantiated when its getInstance() method is called?
 
Ranch Hand
Posts: 213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Riza Aktunc wrote:I know that abstract classes can't be instantiated. So, how come "public abstract class Calendar" has a constructor and get instantiated when its getInstance() method is called?



getInstance() method returns an object of a concrete implementation of Calendar class (like Gregorian calendar).
This is the relevant code I see on opening Calendar class in Eclipse:


 
Riza Aktunc
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It helps to resolve a part of my confusion. I have still a little left.

http://download.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html here the api documentation of Calendar class says that Calendar has 2 constructors.

What are these constructors for? Can an abstract class have constructors? If it can, what is the point?
 
Greenhorn
Posts: 19
Eclipse IDE Firefox Browser Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A concrete class may invoke an abstract (super) class's constructor. You can have a look at the concrete class GregorianCalendar class's constructor:

GregorianCalendar(int year, int month, int dayOfMonth,
int hourOfDay, int minute, int second, int millis) {
super(); //This calls Calender's constructor.

But, you can not directly invoke it: Calendar c = new Calendar() ; //won't compile.
 
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
A constructor doesn't actually create an instance of the class, it's used to initialise it. And an abstract class can still have aspects of it that need initialising. So, as Atul says, the concrete subclass constructor can call the abstract class constructor to do this. In fact, it has to - if you don't make an explicit call then a call to super() is implicitly added.

And Atul - welcome to The Ranch!
 
Atul Shrivastava
Greenhorn
Posts: 19
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Matthew
 
Varun Chopra
Ranch Hand
Posts: 213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every class has a default constructor, in other words yes, every abstract class always has at least 1 constructor.
Why - because abstract class may have instance variables and they can be initialized in the constructor (how will you initialize them otherwise if they are private ??). Calendar class also has instance variables if you look at its code.
Here's the constructor code from Calendar class:



Point - well, see one constructor from Gregorian Calendar below:



It first calls the base class's constructor with super(zone, aLocale); so that base class's instance variables are initialized before its own.
 
Riza Aktunc
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone! And welcome Atul
 
Atul Shrivastava
Greenhorn
Posts: 19
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Riza...I'm not such a new member...its been more than 2.5 years...I didn't post anything to the forum though...
 
reply
    Bookmark Topic Watch Topic
  • New Topic