[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1847
  • Last Modified:

Java class visibility

With regards to the following statement:
Normal (non-inner) classes cannot be made private or protected; they may only be given public or package access.

1)Is this because otherwise no other classes could be derived from it? Are there other reasons?

2) I thought "protected" meant only package access???
0
kellyputty
Asked:
kellyputty
  • 2
  • 2
1 Solution
 
CEHJCommented:
>>1)Is this because otherwise no other classes could be derived from it? Are there other reasons?

It's because they wouldn't be visible.

2. No - they're quite different:


http://mindprod.com/jgloss/packagescope.html
http://mindprod.com/jgloss/protectedscope.html
0
 
kellyputtyAuthor Commented:
thanx
0
 
CEHJCommented:
:-)
0
 
sriki77Commented:
1. There is not point in making a non-inner class private. If there was a non-inner private class it means that the class can be used only by itself and none other. No use of a such an isolated self service class.

2. Protected means access within the package and also by sub-classes. Protected is primarily for inheritence. So it makes sense to have members of a class as protected not the class itself.

here is the accessor hierarchy in Java

Private - within the class
Package - within the package
Protected - within package and sub-classess
Public - every where
0
 
kellyputtyAuthor Commented:
thanx.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now