*
The moose likes Beginning Java and the fly likes The 'DateFormat' abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The Watch "The New topic
Author

The 'DateFormat' abstract class

Varnam Aayiram
Ranch Hand

Joined: Dec 23, 2008
Posts: 88
Hi Folks,

Since abstract classes cannot be instantiated in java, I understand that the following code will not work:


However I noticed that the follwing code works fine, as in there is no compilation error:



I don't understand why. When we use the 'new' operator, are we not creating a new instance of the class, which in this case is not possible since 'DateFormat' is an abstract class? Yet the 2nd line of code works. Will be great if somone is able to explain. Thanks in advance.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
The abstract class is not getting initialized but the array object to hold 6 references to a DateFormat type.
So the array members dfa[0],dfa[1],..dfa[5] can refer to DateFormat child types (SingleDateFormat)

I have tried to put the abstract / array concept in a sample code below.

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Varnam Aayiram wrote:When we use the 'new' operator, are we not creating a new instance of the class,

Correct. In the code you posted, the class of which a new instance is created is DateFormat[] -- not DateFormat.


luck, db
There are no new questions, but there may be new answers.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Varnam Aayiram wrote:When we use the 'new' operator, are we not creating a new instance of the class,


Correct, and that class is DateFormat[]. Note that creating an array of X does not create any X objects. I just creates a bunch of references to type X that are all initially null.

EDIT: Waayyyyy to slow, and yet I could swear there were no answers when I hit Reply.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The 'DateFormat' abstract class
 
Similar Threads
About DateFormat from K&B
DateFormat
Regarding Exceptions!
Date formatting
how do abstract classes like NumberFormat call the methods from their subclasses?