Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The 'DateFormat' abstract class

 
Varnam Aayiram
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic