GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help me on this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help me on this "constructor" question!" Watch "Help me on this "constructor" question!" New topic
Author

Help me on this "constructor" question!

Ben Gao
Greenhorn

Joined: Feb 19, 2002
Posts: 8
Determine the result of attempting to compile and run the following code:
class A {
public int Avar;
public A() {
System.out.println("AAA");
doSomething();
}
public void doSomething() {
Avar = 1111;
System.out.println("A.doSomething()");
}
}
public class B extends A {
public int Bvar = 2222;
public B() {
System.out.println("BBB");
doSomething();

}
public void doSomething() {
System.out.println("Bvar=" + Bvar);
}
public static void main(String[] args) {
new B();
}
}
The output is:
AAA
Bvar=0
BBB
Bvar=2222

My questions are:
why "A.doSomething()" is not output?
Why "Bvar=0"?

Thank you!
Benben
Jennifer Wallace
Ranch Hand

Joined: Nov 30, 2001
Posts: 102
This discussion deals with the same
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014871
[ February 19, 2002: Message edited by: Jennifer Wallace ]
[ February 19, 2002: Message edited by: Jennifer Wallace ]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Benben
Welcome to the Java Ranch, we hope you’ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please change your display name to an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!


Dave
 
GeeCON Prague 2014
 
subject: Help me on this "constructor" question!