• 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

Constructors

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Test2 is a program featured in 2 questions in Simon Roberts' certification book. Base2 has 2 constructors that are called from Test2. I don't know how to use these. How can I print the variables that are in Base2 so I can see what is happening?
One more question. Why are these declarations called constructors even though Base2 b2 = new Base2(); is also called a constructor, but is a completely different thing?
Thanks! Doug
class Base2 {
Base2() { }
Base2(int i, int j) {
i = 65;
j = 43;
}
}

class Test2 extends Base2 {
public Test2() {
}
public Test2(int j, int k) {
super(j, k);
j += 40;
k >>= 2;
}
}
 
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doug,
It appears that only a portion of the code From RHE appeared in your posting. You ask "How can I print the variables that are in Base2 so I can see what is happening?" But the way Base2 is defined in your post, it has no variables. It only has two constructors.
I tried to amend the code below so you could get the concepts.
To use a class's constructor, you create an instance of that class (usually using the "new" operator). The constructor for the base class is always called, either explicitly (using "super" ) or implicitly.


You also ask "Why are these declarations called constructors even though Base2 b2 = new Base2(); is also called a constructor, but is a completely different thing?"
The methods declared ARE the constuctors.
Base2 b2 = new Base2();
is an example of calling Base2's constructor using the "new" operater.
Hope this helps.
Stephanie
 
Douglas Wolfinger
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's great help! Thanks again Stephanie. I posted the programs verbatim from p. 195 & 196 in the ch. 6 review questions. The questions basically (loosely?) refer to p. 180 - 182.
 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic