• 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 level

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference between default and protected access level.
can we aceess the protected member (defined in superclass )using the instance of superclass
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
default:
package level access

protected:
we can access beyond the package if the accessing class excends the protected member's class..
 
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the subclass is in a different package then we cannot access the protected member using the super class reference. we can access it only through inheritance. trying to access using super class reference gives compiler error
 
Ranch Hand
Posts: 643
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you can access a protected member in a derived class but then protected member becomes the private in the derived class itself.


Thanks.
 
akash azal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that is ok
but my doubt is
for example

package cert
public class parent{
protected int x=9;
}
packge other
import cert.parent
class child extends parent{

system.out.println("xxxxxxx"+x);
parent p=new parent();
system.out.println(p.x);///////////here it is giving compiler error
??y
}
 
akash azal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your valuable replies
thanks barun now its clear
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is where you see the Object oriented ft..
You cannot access a protected member in other package directly. You have to use inhertance.
You have to use the subclass objecy...
system.out.println("xxxxxxx"+x);
Here the implict this(subclass object) is allowing you to access
 
reply
    Bookmark Topic Watch Topic
  • New Topic