GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B chapter 2 question 11 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 "K&B chapter 2 question 11" Watch "K&B chapter 2 question 11" New topic
Author

K&B chapter 2 question 11

Rajnish Mishra
Greenhorn

Joined: Jul 14, 2010
Posts: 3

The question is as follow


what is result?

A. subsub

B. sub subsub

C. alpha subsub

D. alpha sub subsub

E. compilation fails

F. An exception is thrown at run time




offilcial ans is ..

C is correct. Watch out, Sub SubAlpha extends Alpha ! Since the code does not attempt to make a SubAlpha , the private constructor in SubAlpha is Okey.




I think it is as I expected that s is a static variable and can't be inherited by its subclasses .. Am I right ???


or any diffrent logic is involved here please clear my problem.. thanks..

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Hi Rajnish, welcome to javaranch.

Rajnish please Use Code Tags when you post a source code. That way your code looks formatted. Unformatted code is hard to read. You can add code tags by wrapping your code in [code] [/code] tags. You can edit your message using button and then add code tags to it.

I think you forgot to put inheritance in your code, none of your classes extend Alpha class...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Rajnish Mishra
Greenhorn

Joined: Jul 14, 2010
Posts: 3

Thanks Ankit sir I have updated my question please try to figure out my problem and provide a solution .
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

You've broke the inheritance by private constructor of your SubAlpha class!

EDIT : OP's code is corrected!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Vicky Mehta
Greenhorn

Joined: Jul 15, 2010
Posts: 15
I copy-pasted the code and have been able to compile and execute it without any errors and output being
alpha subsub
Is it anything to do with the compiler version or?....


Preparing for SCJP
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Vicky Mehta wrote:I copy-pasted the code and have been able to compile and execute it without any errors and output being
alpha subsub
Is it anything to do with the compiler version or?....


Your answer is correct. No problem in the codes.

It's corrected!
Mark Moge
Ranch Hand

Joined: Mar 14, 2009
Posts: 96

Rajnish Mishra wrote:The question is as follow

I think it is as I expected that s is a static variable and can't be inherited by its subclasses .. Am I right ???
or any diffrent logic is involved here please clear my problem.. thanks..


If a superclass has an instance variable or static variable then every subclass has this variable.

Only trick in this question is that the SubSubAlpha extends only the Alpha (and doesn't extend the SubAlpha). When new SubSubAlpha is creating the SubSubAlpha constructor calls Alpha constructor which adds "alpha" to the s String, after that SubSubAlpha adds " subsub" to the s and in a next line of the program the s variable i shown as "alpha subsub"


SCJP6 | http://programmerutilities.blogspot.com/
Rajnish Mishra
Greenhorn

Joined: Jul 14, 2010
Posts: 3

Thanks you all I think my problem is resolved .

Answer is that every variable (static/nonstatic) is inherited by the subclass hence it can be accessed from the subclass.
 
GeeCON Prague 2014
 
subject: K&B chapter 2 question 11