Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access modifier for class and Methods

 
Rami Najjar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I just compere Access modifier for methods and Access modifier for classes in the first time i say its same for Methods or Classes
but when i compare Default access i see its not.

Default (Package private) modifier Access for methods mean that any class in the same package can access that methods or variable.
Default (Package private) modifier Access for classes indicates the class can be accessed only by a subclass or class within the same package.

i read and learn the first sentences when I study about methods design, the second one when I start study about class Design.
i thank there are something wrong because the second sentences its define the protected not default because its also indicates for subclass.




Note : i read this second sentences (Default (Package private) modifier Access for classes) in (Java OCA 8 Programmer I Study Guide) book.
 
Campbell Ritchie
Sheriff
Posts: 51413
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second type sounds like protected access. Look here in the Java™ Tutorials for more details.
 
Kavi Shankar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Classes has only Default and Public access. Correct me If I am wrong.
 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kavi Shankar wrote:Classes has only Default and Public access. Correct me If I am wrong.


For top-level classes, yes. But nested classes (classes that are members of other classes) can have protected and private access as well.
 
Campbell Ritchie
Sheriff
Posts: 51413
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A top‑level package private class is not accessible to subclasses in different packages. What you said about subclasses either means you have confused it with protected access or you have copied it wrongly from somewhere.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic