• 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

Not convinced with K&B solution for couple of questions, please help

 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am not convinced with the answers in chapter 2 [SCJP 1.5,K&B] Self Test questions 7 and 9.
Question 7:


So in the above question we need to insert code to make it compile.
My solution is :
Just add no-arg AgedP().
public AgedP() {

}
K&B solution is
a) Add no-arg AgedP().

b) And also explicitly invoke the default AgedP() in Kinder(int x) using
super().

Since the compiler will implicitly put super() in Kinder(int ) there is no need for us to insert super() in Kinder(int )
Please advice?

Question 9:
Given below code, Choose correct option

Answer is : Woop is-a Zing and Woop has-a Hmpf
Here first part of the answer Woop is-a Zing is correct and straight-forward because Woop extends Zing.
Now second part Woop has-a Hmpf will be correct if Woop inherted the Hmpf variable 'h'.
a) Does inheritance apply to instance variables also?
b) Isn't instance methods are the only thing that are inherited by
subtype?

I verified the second part by writng the below method in Woop class
and to my surprise i found it working which verifies that the answer
is correct.


So does child class inherits non-private instance variables along with non-private methods from parent class?
Please advice.
Thanks
Deepak
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepak Jain:
So does child class inherits non-private instance variables along with non-private methods from parent class?

Generally yes. But if the child class is not in the same package as the parent class, it doesn't inherit the members with default access.
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Generally yes. But if the child class is not in the same package as the parent class, it doesn't inherit the members with default access.



Well! thats about access control, Offcourse if you have default access the memebers will not be inherited by subclasses outside the parent class package.

Answer to first question?
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepak Jain:
Answer to first question?

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


Since the compiler will implicitly put super() in Kinder(int ) there is no need for us to insert super() in Kinder(int )



In the answer of exercise in book:
"The line cannot be left blank, as the parentheses are already in place"

Therefore, your answer is correct. But the insert super() is need for the exercise.
Alexsandra
 
This is my favorite tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic