• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Abstract Class doubt

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

can anyone explain from the below code
what is being done in lines 4 and 5?


Thanks
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
overriden methods are called from base-class constructors, modifying Test.count. After base class constructor ends, non-static initializers of sub-class (Test) are run, setting the Test.count to 0.

your code: (modified to get it compile-able)


output:

Current count = 1
Current count = 2
Current count = 1
Current count = 2
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Here's what happening.

1. At 'point 1', the object is being instantiated.
2. It calls the Test class' constructor ( which is the default one ).
3. It inturn calls the base class' constructor.
(Until the base class' constructor completes, the instance variables of the derived class will remain uninitilized. )
4. The abstract method countPlusPlus() gets called
5. which calls the derived class' method.
6. It increments the value of count to 1
7. again the same method is being called, therefore the value of count = 2.
8. Now the base class' constructor completes and before the end of the derived class' constructor, the value of count gets (re)initialized to 0.
9. All these happening at "Point 1"
10. Then at Point 4 & 5, when the method countPlusPlus() gets called two times, the value of count gets incremented to 1 and then to 2.

The main point is the reset of the private instance variable after the call to base class' constructor and before the end of the derived class' constructor.

Try the modified version of the above code to better understand it.



Also, try leaving the instance variable uninitialized.
ie. private int count;
In this case, you will see the output as

Current count = 1
Current count = 2
Count from constructor :2
Current count = 3
Current count = 4
[ May 16, 2007: Message edited by: M Krishnan ]
 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply John .

thanks alot krishnan, for explaining line by line.
I got the point now

I was seeing the problem with abstract perspective only, did not think from constructor chaining.
how many points we have to see in each question.
i am finding it is tough , but still getting hopes





from the statement what you have given above I have understood as below,
count is being called with 2 different objects.
thats why , when it is coming to Test class's object it reintializes.

if we declare private int count; as
static then the o/p will be
1
2
3
4

if you mean to say something else please clarify me,
Thanks
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
the solution to this as explained by krishnan is fine.
but the actual output is :
1
2
2
7
8
i have run it and seen the output.
In my view the explaination goes like this:

when you load a class the static variables get intialised and after the constructor the instance variables get intialised.
In the above code,
the initialisation doesnt happen before the constructor of the derived class is completed and hence the result 1 and 2
But when it is complete, it gets intialised to 6 and then it gets incremented(through the call of the method) and we get the output 7 and 8.

And yeah if you declare it as static then it becomes a class variable and hence gets intialised when the class is loaded.

Really a good concept.
thanks to all for giving such a good concept.

 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Krishnan,

Further Explanation for my question required, pleas help.
it is confusing me littile bit, first clarify my answer below.
then I'll proceed to further.

if the code is like this



1) the count value will be initially 0.
2) when calls countPlusPlus() first time from super constructor, prints "Current count = 1"
3) when calls countPlusPlus() second time from super constructor, prints "Current count = 2"
4) after completing super class constuctor, the 6 will be initialized.
5) prints "Count from constructor 6"
6) when calls countPlusPlus() first time from sub class, prints "Current count = 7"
7) when call countPlusPlus() second time from sub class, prints "Current count = 8"

Ans:
"Current count = 1"
"Current count = 2"
Count from constructor 6
Current count = 7"
Current count = 8




Thanks in advance
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Yes, your understanding and explanation are correct.
[ May 18, 2007: Message edited by: M Krishnan ]
 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Krishnan,
if count is declared as
private static int count=6;
the answer is

means static variables being initialized first then control goes to super class constructor.
am I right?
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats perfectly correct Madhu! Its a good question.

Thanks to M Krishnan for the explanation.

Yes, even though there is a variable with same name and type 'int count' in both the super and sub classes, when invoking the subclass constructor the method in the derived class gets modified incase of the implementation of abstract method.

ONE CHANGE as what Krishnan Said:


The main point is the reset of the private instance variable after the call to base class' constructor and before the end of the derived class' constructor.



Actually the private instance variable *IS NOT* getting reset at all! The output itself shows that.

See the below output:



When the base class constructor is in execution, it calls the implementation of the abstract method which is of derived class, the private instance variable 'count' *of derived* class gets modified. Thats what you see the output in line 1 and line 2.

Look at Line 3. Its invoked as the last line of Derived Class's Constructor. That is, after the base class constructor finishes its execution.

Look at line 4 and 5. These are called because of countPlusPlus() method invoked via the derived class object directly. They reflect the change based out of the latest value of count (which is 2) AND NOT on its initial value (which is 0 in this case as its left uninitialized)

It shows that the private instance variable of the derived class is NOT RESET after the call to base class constructor and before the end of derived class constructor.



The actual answer and main focus should be on line 4 and 5 of the original question posted by madhu was invoking the implementation of abstract method present in the derived class whose object is being used to invoke the constructor! Is that right?



HtH.
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Actually the private instance variable *IS NOT* getting reset at all! The output itself shows that.



Let me try to explain once again.

In my quote,


The main point is the reset of the private instance variable after the call to base class' constructor and before the end of the derived class' constructor.



I said 'reset' of the private instance and not initialization of private instance.

The output you see is when the private instance variable was left uninitialized.
ie. private int count; //uninitialized in the derived

If this is the case, it will retain the value set by the super class during the reentry in to the methods counterPlusPlus().

Try this:
version 1: with derived class' variable uninitialized:

Output in this case will be:
Current count = 1
Current count = 2
Count from constructor :2
Current count = 3
Current count = 4

Version 2:
with derived class' variable set to some value (in this case 0 )


Output in this case will be

Current count = 1
Current count = 2
Count from constructor :0 //line 3
Current count = 1
Current count = 2

Note, at line 3, the derived class' constructor resetting the value of 0.

Hope this helps.
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


means static variables being initialized first then control goes to super class constructor.
am I right?



Yes. see this eg:


The output:

Daddy's static block . i=100
Son's static block. j=999
Daddy's instance init block
Daddy's constructor
Son's Instance block
Son's Constructor
[ May 21, 2007: Message edited by: M Krishnan ]
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats really nice M Krishnan. Thanks for pointing out the difference which holds a minute difference.



Can we conclude like this?


The main point is the reset of the initialized private instance variable after the call to base class's constructor and before the end of the derived class's constructor.



Note: We should not forget the scenario where it applies for invoking the implementation of abstract method in the subclass via parent class constructor!
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic