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

Putting it all together

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There was a Maryland programmer,
Who read the cert book until slumber,
Then after bit,
To show he got-it,
He wrote his own class modifier.

The classes were simple and frugal,
More code would not have been fruitful,
His job was to illustrate,
Not to obfuscate,
Small class sizes were much more useful.

He put in just enough print statements,
Not too many too few or too less,
But when it did run,
His brain from skull sprung,
Cuz his code was acting teh stoopids!

So he bought cheap beer and did drink it,
On his harsh code he thought "aww screw it",
But after much puking,
He submitted it thinking,
Perhaps Javaranch will help decipher it.


Here's the code:
class BaseClass{

private String name;

void setName(String name) {
this.name = name;
}

String getName() {
return this.name;
}

BaseClass() {
System.out.println("Base constructor called on the sly");

}

BaseClass(String name) {
System.out.println("Base constructor directly called");
setName(name);

}

void describe(Integer j) {
System.out.println("BaseClass" + getName() + " " + j);
}
}

class ChildClass1 extends BaseClass{

ChildClass1(String s){
System.out.println("ChildClass1 constructor called");
setName(s);
}

void describe(Integer j) {
System.out.println("ChildClass1" + getName() + " " + j);
}
}

class ChildClass2 extends BaseClass{

ChildClass2(String s){
System.out.println("ChildClass2 constructor called");
setName(s);
}


void describe(Integer j) {
System.out.println("ChildClass2" + getName() + " " + j);
}
}

public class TestFor {

public static void main(String... args){

BaseClass[] bcArray = { new BaseClass("One"), new ChildClass1("Two"), new ChildClass2("Three") };

int j = 0;

for(BaseClass b : bcArray) {
if( b instanceof BaseClass)
b.describe(j);

if( b instanceof ChildClass1 )
b.describe(j);

if(b instanceof ChildClass2 )
b.describe(j);
j++;
}
}
}

And here's the output:
Base constructor directly called
Base constructor called on the sly
ChildClass1 constructor called
Base constructor called on the sly
ChildClass2 constructor called
BaseClassOne 0
ChildClass1Two 1
ChildClass1Two 1
ChildClass2Three 2
ChildClass2Three 2

Can anyone help expaly why the Base constructor isn't called after ChildClass2 is initialized?
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I might know
But maybe I am just dumb
It is called before

That was my first attempt at haiku.
[ April 14, 2006: Message edited by: J Sato ]
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can anyone help expaly why the Base constructor isn't called after ChildClass2 is initialized?



Because the BaseClass constructor is supposed to be called before the ChildClass2 constructor.

Henry
 
Mike Van
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:


Because the BaseClass constructor is supposed to be called before the ChildClass2 constructor.

Henry



Henry,

I challenge you to show that is the reason. What you suggest is that if you have two subclasses with the same parent, you would not call the the parent constructor when each of the subclasses is create... that you would only call it once. Can you show that this is in fact what is happening?

<Later I added>

Doh! I see what you're saying. The missing call is the call to the base-class' argument-less constructor. Thanks Anyway!

</Later I added>

v/r,
[ April 14, 2006: Message edited by: Mike Van ]
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
great poems!
 
J Sato
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike,

This is my understanding. If the original question was why doesn't "Base constructor called on the sly" appear in the output after "ChildClass2 constructor called" it is because it appears before it.

------- BaseClass("One") -------
Base constructor directly called
--------------------------------

------- ChildClass1("Two") -----
Base constructor called on the sly
ChildClass1 constructor called
--------------------------------

------- ChildClass2("Three") ---
Base constructor called on the sly
ChildClass2 constructor called
--------------------------------

There is an implicit call to super() in the first line of the constructor for ChildClass2. HTH
 
Mike Van
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bert Bates:
great poems!



public class ThanksPrinter{

private String thanksMessage;

public void setThanksMessage(String thanksMessage) {
this.thanksMessage = thanksMessage;
}

ThanksPrinter() {
setThanksMessage("Thanks!");
}

ThanksPrinter(String msg) {
setThanksMessage(msg);
}

public void printThanksMessage() {
System.out.println(this.thanksMessage);
}

public static void main(String... args) {
String[] thanksMessages = {"Thanks Bert", "Nice book, by the way",
"It causes consternation, constipation,",
"and finally... Certification!"};
ThanksPrinter theThanksPrinter = new ThanksPrinter();
for(String s : thanksMessages) {
theThanksPrinter.setThanksMessage(s);
theThanksPrinter.printThanksMessage();
}
}
}

java -classpath . ThanksPrinter

lather, rinse, repeat
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
constipation ?

 
Mike Van
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bert Bates:
constipation ?



Certification study,
Sitting long hours no rest,
Forgot bio-breaks.
 
Did you just should on me? You should read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic