Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

new keyword doubt in abstract class context

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
of type of what you mentioned in that idiom.

note: List[] and List both are different things
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll give some more detailed examples, if that helps:
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:I'll give some more detailed examples, if that helps:


Thanks Matthew ,it is a good example.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Seetharaman Venkatasamy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic