| Author |
What's the use of non-public top level classes?
|
David Aslan
Greenhorn
Joined: Jun 10, 2005
Posts: 13
|
|
I've never used non-public top level classes before, and can't think of any reason when to use them... I played around with some code even this: It seems that top level class Dumb can even access Verifier's protected member abc even though it isn't a subclass of Verifier!
|
-----<br />SCJP, SCWCD, SCMAD, SCEA, CAPM
|
 |
Jeremy Botha
Ranch Hand
Joined: Feb 16, 2005
Posts: 125
|
|
it can access it because you've marked it as protected. Protected grants access to all implementing subclasses AND classes in the same package. J
|
McFinnigan? Never heard of him. Nobody here but us chickens...<br /> <br />SCJP for Java 1.4<br />SCJD for Java 5.0
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12911
|
|
I've never used non-public top level classes before, and can't think of any reason when to use them...
Non-public top-level classes are visible only to other classes in the same package. When you're writing a piece of software, you might want to create a class that should only be used by the other classes in the package, and which you don't want to be visible in the public API of your software.
It seems that top level class Dumb can even access Verifier's protected member abc even though it isn't a subclass of Verifier!
That is because protected means: accessible from subclasses and from classes in the same package. I don't know if you've previously programmed in C++ or another programming language that has the 'protected' keyword - this is a difference between Java and C++.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
David Aslan
Greenhorn
Joined: Jun 10, 2005
Posts: 13
|
|
I've rarely seen package level methods and functions. In what cases would it be useful to have package level classes? If the utility/helper class were accessing only one class, it would make better sense to make it an inner class of just that one class. The only reason I think may be if we needed to access non public members and functions of more 2 or more classes. If that's the case, it would mean that such a non-public top level class would perform some kind of coordination glue between the other classes.
|
 |
 |
|
|
subject: What's the use of non-public top level classes?
|
|
|