• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

The 'DateFormat' abstract class

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic