• Post Reply Bookmark Topic Watch Topic
  • New Topic

why can't classes be private  RSS feed

 
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. I know that clases cannot be private or protected. Why so?

2. Same goes with interfaces, they cannot be private and protected. I understand that they cannot be private because no other class will be able to implement it then. But why not protected? What if I want only the classes in a particular package to implement an interface?

regards,
vijay.
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Classes can be both private and protected so long as they are defined as inner classes. The rule is that every class file must have exactly one public class declared. After which, though, any number of additional inner classes may be defined including anonymous/static classes.

Consider the following valid code:



You can also define any number of outter-level package classes such as TestFile2 as shown above.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vijay Raj:
What if I want only the classes in a particular package to implement an interface?


That's easily done. Use the default level of access (sometime pronounced
"package-level" access)

Widget will only be visible in package com.acme.tools
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Scott Selikoff:
The rule is that every class file must have exactly one public class declared


It's not a must that a public class be declared in a compilation unit (i.e., a Java source file). There can be, at most, only one public class in it. You can also have one or more default access classes (package access) declared in a compilation unit.
 
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class whose main() will be called by the runtime should be the public class. Right?

Why is there such a rule, can't the runtime call the main() method of a private or a protected class?

regards,
vijay.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That "rule" derives from two other rules:

1. The runtime calls the "main" method of a top-level class.

2. A top-level class cannot be private or protected.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Scott Selikoff:
Classes can be both private and protected so long as they are defined as inner classes.


Almost.
Classes can be either private or protected so long as they are defined as nested classes.

What is the difference between a nested class, an inner class, a local class, an anonymous class and a member class (or interface)?
http://jqa.tmorris.net/GetQAndA.action?qids=67&showAnswers=true
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Morris:


Almost.
Classes can be either private or protected so long as they are defined as nested classes.


Ah, I meant nested, not inner. Terminology sounds so similar to me I suppose.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A private top-level class would be of no use, because noone could ever access it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!