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

Good ques..???

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

whats making this code to give the answer as 5???
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure it compiles?
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before constructor call,first static fields,static blocks are executed(depending on order of definition),then instance fields,instance blocks, super class constructor then actual constructor will be executed.So, before Q028, constructor of B class is called which in turn calls A construtor, before which static field i will be initialized to 0, then in A's constructor , i will be incremented to 1,then called will be returned to B's constructor where i will be incremented to 2,

so before getValue() method was called , i will have 2, to which 3 will be added, getting result of 5.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code cannot compile but if you change the

private B(){i++;}


to public B(){ i++}

the output i am getting is 6 it cannot be 5.
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

As of i am thinking, code should not compile bcz constructor in class B is declared as private.
 
gaurav singhal
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

so before getValue() method was called , i will have 2, to which 3 will be added, getting result of 5.



But before this we have A constructor call which increment the value of i from 2 to 3 and the calling the getValue() method will add 3 to it then it is 6 but it is possible only when the constructor in B is public.

So the finally the value of i is 6
[ March 05, 2006: Message edited by: gaurav singhal ]
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
gaurav singhal
yes you are right.I agree with you
 
Amit Goyal
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Gaurav,

U R Right.
But as of i am thinking, the problem is that why the get() method of class B is called even it is casted to class A and the reference is also of the class A. (pls correct me if i am wrong).
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
gaurav singhal
yes you are right.I agree with you
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

1- change private B() to public B(). As in case of private B(),
Q028 obj = new Q028(); cannot create object of B.

2- Program will print 6, as it is calling get() of B.

ob = (A)obj;

has not affect as here we are doing upcasting during assignment which is implict in nature. In other words
is equivalent to
 
gaurav singhal
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But as of i am thinking, the problem is that why the get() method of class B is called even it is casted to class A and the reference is also of the class A. (pls correct me if i am wrong).



See you are mistaking here the reference is of A but the actual object is of Q028 so during runtime it find the method get() in B becoz Q028 does not override this method and next class in hierarchy is B.

Regards
Gaurav
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Constructor of class B which is B() has private access in it's class (i.e. B) so it is unaccessible to other class for e.g. Q028. When the default constructor of Q028 is called it will give an implicit call to super() which corressponds to the no-argument constructor of B i.e. B() but to call this no-arg constructor B() it should have either public or default access. private access on the B() will prevent it from being called outside the class B.

Please Note >> Constructors are not inherited nor they are overriden. they can only be overloaded in the same class.

Thanks.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello "Kommon Programmer"-

Welcome to JavaRanch.

On your way in you may have missed that JavaRanch has a policy on display names, and yours does not comply with it - please adjust it accordingly, which you can do right here. Thanks for your prompt attention to this matter.

Enjoy your time here.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code won't compile unless the contructor of class B is modified.After that it will give print 6
 
reply
    Bookmark Topic Watch Topic
  • New Topic