Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Protected Access (yes again..yawn!!)

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies for having to ask about something that's been done to death on here but can I just confirm what I think is correct about access modifiers:
Public - Accessible by all classes in any other package.
Protected - Accessible by all classes within the same package AND any other class in another package that subclass's it (so therefore you would need to import the package/class)

Friendly(ie no modifier) - Accessible by all classes within the same package.
Private - Accessible only within the same class.
I hope I've got it right, protected is obviously the thing I'm not 100% about so any clarification is more than welcome
All the best everyone,
Jon
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, just to make sure as your sentence was not clear, we're talking about protected methods or variables, not protected classes. You can't have a protected or private Top-level class.
Also instance methods and variables that are declared protected can be accessed by subclasses in another package ONLY through instances of the subclasses - being inherited - and NOT instances of the super classes.
 
Jon Kidd
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alfred,
Thanks for the reply, much appreciated, apologies yes I do mean methods or variables.
Could you possibly explain


Also instance methods and variables that are declared protected can be accessed by subclasses in another package ONLY through instances of the subclasses - being inherited - and NOT instances of the super classes.


a little more with maybe an example as I'm not 100% clear on what you mean ?
Jon
Ok, just to make sure as your sentence was not clear, we're talking about protected methods or variables, not protected classes. You can't have a protected or private Top-level class.
Also instance methods and variables that are declared protected can be accessed by subclasses in another package ONLY through instances of the subclasses - being inherited - and NOT instances of the super classes.
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK for instance if:
- class Super is in package X and
- method m() is in class Super and is declared protected and
- class Sub extends Super and is in package Y
In the body of class Sub, it's illegal to write such code:
Super superInX = new Super();
superInX.m(); // compile time error
BUT it's totally legal to write:
Sub subInY = new Sub();
subInY.m();
a protected method can be accessed in a subclass in a different package ONLY through the class's inheritance, meaning that it doesn't have access to the method due to the access control only but due to inheritance.
I hope this made it clear.
[ November 20, 2002: Message edited by: Alfred Kemety ]
 
Jon Kidd
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alfred,
Spot on, thanks very much, all written extremely clearly and much appreciated
This is certainly one of the most helpful boards and a big thankyou to all those who take the time and trouble to help myself and others like myself still struggling to come to terms with Java.
Jon
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Access modifiers exist for every memebr (variables and methods) and can explicitly include: public, protected, and private.
If no explicit modifier is used, then "default access" is the access level - which means it has package level accessibility (sometimes called "friendly".
public - this memeber is accessible by all other classes. this assumes that the containing class IS accessible and the containing package of said class is "imported" in the class using or calling that public member.
protected - similar to package ("Friendly") level access PLUS subclasses can access from any other package
package level aka "Friendly(no access modifier keyword used for this) - package level access is implicit if no keyword is used: all classes in the current package have access to the friendly memeber (but to all classes including subclasses, outside of this package, the memebers appear to be "private").
private - most restrictive access. Only accessible from within same class. You can't even inherit private members.
 
Well THAT's new! Comfort me, reliable tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic