• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

why can't classes be private

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 4323
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 17031
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 27286
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 4323
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A private top-level class would be of no use, because noone could ever access it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic