File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Practice Question 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 "Practice Question" Watch "Practice Question" New topic
Author

Practice Question

Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
// Please don't cheat and spoil it for others !
// The output of the following:
// >java BaseTest

[ September 29, 2003: Message edited by: Tony Morris ]

Tony Morris
Java Q&A (FAQ, Trivia)
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
I will not tell my prediction. But it turned out to be wrong. I need more practice...
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
A very good question! My disbelief prevented me from making the right guess because I didn't know how to explain it, so I can't say that I solved it in the first place...
Sachin Tendulkar
Greenhorn

Joined: Sep 20, 2003
Posts: 27
Wasnt too difficult.
public abstract class BaseTest
{
public abstract void test();

public BaseTest()
{
test();
}

public static void main(String[] args)
{
BaseTest bt = new DerivedTest();
}
}

class DerivedTest extends BaseTest
{
int i = 2;

public DerivedTest()
{
i = 3;
}

public void test()
{
System.out.println("i = " + i);
}
}

When the DerivedTest constructor is called it invokes the superclass constructor which in turn invokes its superclass constructor (Object). After the Object constructor returns the test() method is invoked (as defined in the DerivedTest class). Since at this point the variable i has only been declared and not yet been initialized i is printed as zero.
Lalitha Chandran
Ranch Hand

Joined: Jul 03, 2003
Posts: 92
I knew the concept but did a careless mistake......
Asha Sat
Greenhorn

Joined: Sep 29, 2003
Posts: 22
In Inheritance when the variables are initialized? I don't understand how and when the variable i is initialized to 0.
Confused


{ Winners do what losers don't do }
Sreenivasa Majji
Ranch Hand

Joined: Jul 12, 2001
Posts: 224
Very good question.
My guess is it prints 2.
I assume that member variables will be initialized before invoking the constructor ( i knew static variables/blocks will be called before the constructor).
Can somebody clarify me??


Sreenivasa Majji
Aiping Zhou
Greenhorn

Joined: Sep 06, 2003
Posts: 24
I think java lova is right. I tried code and run it. The system did print 0. Why?
After the Object constructor returns the test() method is invoked (as defined in the DerivedTest class). Since at this point the variable i has only been declared and not yet been initialized i is printed as zero.

Two things I don't understand:
1) If the instance member i already be declared at compile time, why this line of code (int i = 2 just finish half of them. I thought this whole line should be finished at compile time.
2) Can a member of a class can be access before finishing construction?
The member test() of the child class DerivedTest was invoked before finishing its construction.
Really confuse now.
Anybody can make this clear? Thanks.
Asha Sat
Greenhorn

Joined: Sep 29, 2003
Posts: 22
Here is the logical reasoning of How i got 0.
The first step in creating an instance of the DerivedTest class is the allocating memory(Heap) for the instance variables of DerivedTest and its superclass, BaseTest.
Next, the instance variables in classes BaseTest and DerivedTest are set to their default values: 0 for integers (null for string variables).
Next, The DerivedTest constructor is called with no arguments. Since DerivedTest class does not have a call to this() or super(), the compiler adds a call to the default constructor, super(), which calls the constructor of the class BaseTest.
Next, Any instance variables in the super class are initialized(in our example there is nothing), the body of BaseTest�s constructor is executed, and the flow of control is returned to DerivedTest class. In this example test() method associated with Derived class is called using dymanic lookup. The instance variable i is not initialized as 2 yet (see next section for more). It has the default initialization value 0. Hence , it is printed as 0.

Next,control is returned to DerivedTest class from the super class, i is assigned the value 2, and the rest of the body of Simple class constructor is executed, setting instance variables i to 3 . Finally, control is returned to main.
Hope this clear up about
a) When the instance variables of subclass are defaultly initialized and explicitly initialized
b)When the instance variables of super class are defaultly initialized and explicitly initialized .
Asha
[ September 30, 2003: Message edited by: Asha Sat ]
Sachin Tendulkar
Greenhorn

Joined: Sep 20, 2003
Posts: 27
Here is what the JPL (Arnold, Gosling, Holmes) has to say about object creation.
When an object is created, memory is allocated for all its fields, including those inherited from the superclasses and those fields are set to default initial values. After this the construction has three phases
1. Invoke the superclass constructor.
2. Initialize the fields using their initializers and any initilization blocks.
3. Execute the body of the constructor.
Now lets apply these steps to the question at hand.
When the statement new DerivedTest() is executed. memory is allocated to all its fields including those inherited from its superclass (BaseTest in this case) and they are all set to their default values. Hence at this point i=0.
Next the superclass constructor is invoked. That is the constructor for BaseTest is invoked.
Note again that this will in turn invoke the superclass constructor which is Objects constructor. Since Object has no superclass at this point the superclass invokation returns.
After the object constructor returns the method test() in the BaseTest constructor begins execution.
This method has been defined in DerivedTest. All this time we can see that we are only at stage 1 of the object creation step above.
i has been defaulted to 0 since it is an integer and this is printed by the SOP in test();
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
The explanation by Asha Sat is clear and accurate.
No point reiterating it.
Good luck in the exam !
Praveen Mathur
Greenhorn

Joined: Sep 21, 2003
Posts: 18
I just verified what Asha and Java lova explained to us:
public abstract class BaseTest
{
public abstract void test();
public BaseTest()
{
System.out.println("Super Class Constructor called");
test();
}
public static void main(String[] args)
{
BaseTest bt = new DerivedTest();
}
}
class DerivedTest extends BaseTest
{
int i;
{
i=2;
System.out.println("Variable Initialization");
}
public DerivedTest()
{
i = 3;
System.out.println("Subclass constructor called");
}
public void test()
{
System.out.println("i = " + i);
System.out.println("Test Method Called");
}
}
Output after compilation is:
Super Class Constructor called
i = 0
Test Method Called
Variable Initialization
Subclass constructor called


"The self-controlled soul, who moves amongst sense objects, free from either attachment or repulsion, he wins eternal Peace." - Bhagavad Gita
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Practice Question