• 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

Dan's ques(Ch 6- Q21) Private access

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help me. I want to know how the superclass mtd is called. It cannot be overriden. But isn't it illegal to acess the private mtd of superclass.
Also when subclass has its own mtd why isn't it called?



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

I might not be able to explain it easily... I'm understanding this as I write it, but i did test it.

printS1S2() is present in S because it inherits it. When it's called on the S instance, it actually execute the method in R (the code is defined in R right, it was not overriden in S).

I guess you will agree that at first, printS1S2() from R is called.
Your child class does not override it, and it is defined as protected in R, so its accessible from the package (and would also be by its child classes like S, even if it was in another package...).

Then, in printS1S2() we have 2 call :

- printS1();// This guy is private to R, and for this reason could not be overriden in S : S simply defined another methode with the same name. So, this actually call the private method in R.

- printS2(); // because this one was overriden in S, the S method is called here.

So you get the result : R.printS1 S.printS2


If you change printS1 from R and S to protected, both method from S would then be called.

Alex
 
Kalyani Marathe
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was thinking:
the mtd prints1() call will be : this.printS1()--> newS().printS1

I want to clarify why it is not happening. Also how the private acess of superclass is allowed.

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

By the rules of dynamic method binding, when you call printS1S2, on object of type S, the method in type R is executed. Now, with in that method, a call to printS1() at line 1, will call its (type R's) own private version of the method and not the one defined in S (had it been overridden with a widening modifier, the overridden method would get executed) and since printS2 is overridden, the overridden method in class S is executed and hence the result. Hope that helps.
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
---------------------------------------
the mtd prints1() call will be : this.printS1()--> newS().printS1

I want to clarify why it is not happening. Also how the private acess of superclass is allowed.
---------------------------------------

hi,

Im not sure I understand what you mean by "private access of superclass".

- new S().printS1S2(); //prints1s2 is not private, its protected, so package and child classes have access to/inherits it.

Then, when in printS1S2, you are inside the class R so you have access to the private printS1.So again, there is nothing wrong with this call, because it was not called from outside. This function could not have been called directly from S because its private.

Can you give more detail on what you mean by "private access of superclass". which statement exactly.

regards,
Alex
[ April 26, 2005: Message edited by: Alex Turcot ]
 
Kalyani Marathe
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Correct me if I am wrong:

as call is to printS1S2 which is not in S so called from parent class R. As we are in now parent class and calling printS1(which is private) so it has to look for it in parent as call cannot go down to child and also cannot see the childs private mtd.

Please correct me if I have implied wrong.

Thanks Alex and Rahul.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rahul Bhosale:
By the rules of dynamic method binding, when you call printS1S2, on object of type S, the method in type R is executed. Now, with in that method, a call to printS1() at line 1, will call its (type R's) own private version of the method and not the one defined in S (had it been overridden with a widening modifier, the overridden method would get executed)



Rahul, I agree with much of your post, but the statement "(had it been overridden with a widening modifier, the overridden method would get executed)" is not correct, if you mean that the overriding method in the subclass would be executed. Since printS1() in R is private, the call to printS1() from within a method of R will not use dynamic binding to determine what method to call. It will just use the private method in R, even if the printS1() method in S is public. Try this code:



The output is still
R.printS1 S.printS2

Because R's method printS1() is not accessible from within the subclass S, that method is not inherited by S. So making a method in S with the same signature is not overriding the method in R, since that method was never inherited.

and since printS2 is overridden, the overridden method in class S is executed and hence the result. Hope that helps.



Here, I think you mean the overriding method, not the overridden method. The overriding method is in the subclass. The overridden method is in the superclass.
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kalyani,

I would say you are partly right.
ok, printS1S2 does not call S's printS1 because its private and it cannot see it, while it can see R's printS1. So it has no choice but to call R's printS1.

However, printS1S2 choose to call printS2 from S instead of R, even though both were visible. Because of this, I like to think that it depends on if the method was overridden or not.

Also, imagine that printS1S2 would refer to a 'private int i' (declared in R).
the variable 'i' would not be inherited in S.

So when you call printS1S2, you cannot rely on S to deal with this, because you don't know what private variables printS1S2 is dealing with. So, you must ask R to take care of this, because he is certainly the only one who has access to his private stuff. Because of this, printS1S2 is executed as if you were in R, and then you can only see printS1 from R. This is just the way I think the compiler thinks, it helps me to see some kind of logics in that.

Hope that helps...

Alex
[ April 26, 2005: Message edited by: Alex Turcot ]
 
Kalyani Marathe
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the logic now. Thanks everybody for the help.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic