aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Good ques..??? 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 "Good ques..???" Watch "Good ques..???" New topic
Author

Good ques..???

cs singh
Ranch Hand

Joined: Dec 28, 2005
Posts: 36

whats making this code to give the answer as 5???
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Are you sure it compiles?
bnkiran kumar
Ranch Hand

Joined: Mar 02, 2006
Posts: 176
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.


Kiran Kumar.
gaurav singhal
Ranch Hand

Joined: Nov 18, 2005
Posts: 135
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.
Amit Goyal
Ranch Hand

Joined: Feb 21, 2006
Posts: 95
Hey,

As of i am thinking, code should not compile bcz constructor in class B is declared as private.
gaurav singhal
Ranch Hand

Joined: Nov 18, 2005
Posts: 135
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

Joined: Mar 02, 2006
Posts: 176
gaurav singhal
yes you are right.I agree with you
Amit Goyal
Ranch Hand

Joined: Feb 21, 2006
Posts: 95
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

Joined: Mar 02, 2006
Posts: 176
gaurav singhal
yes you are right.I agree with you
Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 324
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


"Know where to find the solution and how to use it - that's the secret of success."
gaurav singhal
Ranch Hand

Joined: Nov 18, 2005
Posts: 135
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
Kommon Programmer
Greenhorn

Joined: Mar 07, 2006
Posts: 1
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.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41861
    
  63
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.


Ping & DNS - my free Android networking tools app
Anuj Kish
Greenhorn

Joined: Mar 07, 2006
Posts: 1
The code won't compile unless the contructor of class B is modified.After that it will give print 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Good ques..???