• Post Reply Bookmark Topic Watch Topic
  • New Topic

Five-Minute Mystery HeadFirstJava Book page 268  RSS feed

 
Bart Boersma
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello CodeRanch,

I am new here so excuse me for any mistakes.

I am reading the book HeadFirstJava and got stuck on one of the Five-Minute Mysteries. I have a few questions:
1. In the main the V2Radiator v2 = new V2Radiator(aList) gets aList as an argument. Is this because the constructor: V2Radiator(ArrayList list) takes an arraylist as argument?

2. What  happens when putting an ArrayList as argument in a constructor? The object is not automatically put into the ArrayList (no add. statement) so what does it do?

3. The class V3Radiator extends the class V2Radiator. This constructor also has to take an ArrayList as argument because it extends V2Radiator (V3Radiator(ArrayList lglist)). However it also states: super(lglist). The exercise solution states that in that particular sentence (super(lglist)) the for loop in class V2Radiator run again. Why does it run again and can I leave this super(lglist) part out of the code if I don't want it to run again?

CODE:

 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Bart!
I am a beginner just like you but here is what I think:
1. You said it right, the constructor dictates how the object will be created. What comes after new is a constructor call.
2. alist will have 5 objects added, this is dictated by the for in the V2Radiator constructor.
3. Constructors are not inherited. Even if V2Radiator is extended by V3Radiator, the constructor from V3 will not extend that from V2. This is why we need to call super() so it will explicitly execute everything from V2, of course you can omit it if you do not want that.
 
Bart Boersma
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Sergiu!

One thing that has not fully cleared the fog:
What happens when I state:
ArrayList aList = new ArrayList();
V2Radiator v2 = new V2Radiator(aList);
 
Bart Boersma
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Sergiu!

One thing that has not fully cleared the fog:
What happens when I state:
ArrayList aList = new ArrayList();     --> Create new ArrayList with reference variable aList.
V2Radiator v2 = new V2Radiator(aList); --> Create new V2Radiator with reference variable v2 --> However, what does aList do? It gives this object an ArrayList?

Could you please explain this to me?

Thanks in advance!
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The aList is passed into the constructor as the parameter.
So look at what happens to that parameter in the constructor.
 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bart Boersma wrote:Thank you Sergiu!

One thing that has not fully cleared the fog:
What happens when I state:
ArrayList aList = new ArrayList();     --> Create new ArrayList with reference variable aList.
V2Radiator v2 = new V2Radiator(aList); --> Create new V2Radiator with reference variable v2 --> However, what does aList do? It gives this object an ArrayList?

Could you please explain this to me?

Thanks in advance!

Yes, the object needs aList to be created. aList is passed as a parameter to the object's constructor, which results in aList being populated with undeclared objects.
 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:The aList is passed into the constructor as the parameter.
So look at what happens to that parameter in the constructor.

aList has 5 undeclared objects added to it. I don't see what's the point with that happening?
 
Bart Boersma
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dave and Sergiu, the air has been cleared I assume!

I will put this thread to resolved.

Have a nice day.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!