• 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

Access Modifier/Package

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from an online Mock Exam - http://www.akgupta.com/Java/mock_exam.htm






The say() method defined in Super.java has a protected modifier
That would mean it's accessible within the it's own package and in the subclass which may be outside the package.

But i still get the error in line 7 and 8 , something to do with protected modifier.

I think I am missing something ..just dont know what!
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
The line 7 and 8 gives error because you can acces a protected method only via class which has inherited it and not by directly creating a object of a class in a different package.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code is giving Compiler Error because the method say() in Super is available to SubTwo & SubOne via inheritance though its access is protected.
So you do not need any reference to access them, they can be access just be their name.
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Harshit Rastogi:
Hi
The line 7 and 8 gives error because you can acces a protected method only via class which has inherited it and not by directly creating a object of a class in a different package.



Let me rephrase it
The line 7 and 8 gives error because you can acces a protected method only via class object (SubTwo) which has inherited it and not by directly creating a object of a class directly (Super, SubOne)in a different package.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have slightly modified the code.




Thanks.
I kind of rememeber something about accesing protected members through inheritance and not by reference...
But i still can't the right statement for acessing say() in Super and SubOne class.

If i could get some help with that....
[ September 11, 2008: Message edited by: Nabila Mohammad ]
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the code you provided at the start, you can access the say() of Super by say("Java")...
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@NABILA MOHAMMAD
Remember that a protected class members can be always accessed thorough inheritance but not through references.
Have a look at the following code:



So the say method cannot be accessed through reference.
Can you spot the difference now?
For more explanations, goto K&B: page 34-39..

Have a look at this url for more.
[ September 12, 2008: Message edited by: Paul Somnath ]
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Somnath:
@NABILA MOHAMMAD
Remember that a protected class members can be always accessed thorough inheritance but not through references.
Have a look at the following code:



So the say method cannot be accessed through reference.
Can you spot the difference now?
For more explanations, goto K&B: page 34-39..

Have a look at this url for more.

[ September 12, 2008: Message edited by: Paul Somnath ]



Hi Paul,
Thanks for the explanation.
I had already gone through the website as well as the K&B.

However they have both mentioned accessing variable in the Parent class-
which has not been re defined in the child class.

If i have defined a say() method in all the three classes - Super,Subone and SubTwo
Or a variable s in all the three - that case how do I call the variable or method in Super or SubOne class because it will always give the value of SubTwo.I mean is it even possible!?
[ September 12, 2008: Message edited by: Nabila Mohammad ]
 
You showed up just in time for the waffles! And 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