Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Practice Question

 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// Please don't cheat and spoil it for others !
// The output of the following:
// >java BaseTest

[ September 29, 2003: Message edited by: Tony Morris ]
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will not tell my prediction. But it turned out to be wrong. I need more practice...
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I knew the concept but did a careless mistake......
 
Asha Sat
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Inheritance when the variables are initialized? I don't understand how and when the variable i is initialized to 0.
Confused
 
Sreenivasa Majji
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Aiping Zhou
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The explanation by Asha Sat is clear and accurate.
No point reiterating it.
Good luck in the exam !
 
Praveen Mathur
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic