• 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

K&B chapter 2 question 11

 
Greenhorn
Posts: 3
Eclipse IDE Java Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..

 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Rajnish Mishra
Greenhorn
Posts: 3
Eclipse IDE Java Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit sir I have updated my question please try to figure out my problem and provide a solution .
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've broke the inheritance by private constructor of your SubAlpha class!

EDIT : OP's code is corrected!
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?....
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Ranch Hand
Posts: 101
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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"
 
Rajnish Mishra
Greenhorn
Posts: 3
Eclipse IDE Java Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
We noticed he had no friends. So we gave him this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic