It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes new keyword doubt in abstract class context 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "new keyword doubt in abstract class context" Watch "new keyword doubt in abstract class context" New topic
Author

new keyword doubt in abstract class context

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

The new keyword allocates new objects and initialize them.


DateFormat is an abstract class so why we're using new keyword here.How it is correct ?


Tell the difficulties that i am difficult.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

What you are creating is an array of length 6. Each element of that array will hold a reference which IS-A DateFormat.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Anayonkar Shivalkar wrote:What you are creating is an array of length 6. Each element of that array will hold a reference which IS-A DateFormat.

I hope this helps.


What does it mean,i am not getting.
The new keyword allocates new objects and initialize them.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

DateFormat[] dfa=new DateFormat[6];

non-base type variable are references , dfa itself is not an array. it is a DataFormat type which points to an array of type of DateFormat. which can holds an object which extends DateFormat.

you can also do the same with interface in java.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Seetharaman Venkatasamy wrote:DateFormat[] dfa=new DateFormat[6];

non-base type variable are references , dfa itself is not an array. it is a DataFormat type which points to an array of type of DateFormat. which can holds an object which extends DateFormat.

you can also do the same with interface in java.


I read in book that when JVM encounters new keyword it creates object, and this quote also says same thing.
The new keyword allocates new objects and initialize them.



Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

The new keyword allocates new objects and initialize them.

Exactly. With new keyword, you are creating and initializing an array object.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

of type of what you mentioned in that idiom.

note: List[] and List both are different things
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Anayonkar Shivalkar wrote:
The new keyword allocates new objects and initialize them.

Exactly. With new keyword, you are creating and initializing an array object.


No, you can't create object of DateFormat abstract class.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Seetharaman Venkatasamy wrote:of type of what you mentioned in that idiom.

note: List[] and List both are different things


Thanks
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Seetharaman Venkatasamy wrote:of type of what you mentioned in that idiom.

note: List[] and List both are different things


So what happened when we create an array object of a abstract class ?.thanks
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

saloni jhanwar wrote:
Anayonkar Shivalkar wrote:
The new keyword allocates new objects and initialize them.

Exactly. With new keyword, you are creating and initializing an array object.


No, you can't create object of DateFormat abstract class.

Let me repeat:
With new keyword, you are creating and initializing an array object. Not a DateFormat object.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Anayonkar Shivalkar wrote:
saloni jhanwar wrote:
Anayonkar Shivalkar wrote:
The new keyword allocates new objects and initialize them.

Exactly. With new keyword, you are creating and initializing an array object.


No, you can't create object of DateFormat abstract class.

Let me repeat:
With new keyword, you are creating and initializing an array object. Not a DateFormat object.


So what happened when we create an array object of a abstract class ?complete process please. thanks
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

saloni jhanwar wrote:So what happened when we create an array object of a abstract class ?

Again, you/we are not creating an array of objects.

dfa is a reference.
It refers to an array.
The array contains 6 references of type DateFormat.
Each reference will refer to an object which IS-A DateFormat.

I hope this helps.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Anayonkar Shivalkar wrote:
saloni jhanwar wrote:So what happened when we create an array object of a abstract class ?

Again, you/we are not creating an array of objects.

dfa is a reference.
It refers to an array.
The array contains 6 references of type DateFormat.
Each reference will refer to an object which IS-A DateFormat.

I hope this helps.


you are just ignoring my question.


Anayonkar Shivalkar wrote:

saloni jhanwar wrote:

Anayonkar Shivalkar wrote:

The new keyword allocates new objects and initialize them.


Exactly. With new keyword, you are creating and initializing an array object.



No, you can't create object of DateFormat abstract class.


Let me repeat:
With new keyword, you are creating and initializing an array object. Not a DateFormat object.



So what happened when we create an array object of a abstract class ?complete process please. thanks
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4338
    
    7

saloni jhanwar wrote:So what happened when we create an array object of a abstract class ?complete process please. thanks

Exactly the same as when you create an array object of anything else. It creates an empty array of the given size that can later be filled with values. If it's an array of a primitive type all the entries are initialised to 0 (or the equivalent), and if it's an array of a reference type they're all initialised to null.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

saloni jhanwar wrote:you are just ignoring my question.

Because your question is wrong. You are not creating array of objects. You are creating array of references. And as mentioned, those are references of type DateFormat, which can refer to something which IS-A DateFormat.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4338
    
    7

I'll give some more detailed examples, if that helps:
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Anayonkar Shivalkar wrote:
saloni jhanwar wrote:you are just ignoring my question.

Because your question is wrong. You are not creating array of objects. You are creating array of references. And as mentioned, those are references of type DateFormat, which can refer to something which IS-A DateFormat.


yes ,sorry array of references but array itself is an object and i think because of we had used new keyword.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Matthew Brown wrote:I'll give some more detailed examples, if that helps:


Thanks Matthew ,it is a good example.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

dont know we are in high instruction or no...

as of now, you just think like this:
new X[] is possible where X is an interface or abstract and new X() not possible where X is an interface or abstract.

hope once your experience grow ... you will get this ..it is not too long..may be after 1 hour, 1 day or 1 month or 1 year ... they are short period.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

thanks Seetharaman Venkatasamy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: new keyword doubt in abstract class context
 
Similar Threads
Date Parsing Issue...
The 'DateFormat' abstract class
DateFormat question
Preparing for the SCJA
how do abstract classes like NumberFormat call the methods from their subclasses?