• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Accessibility Modifiers for classes

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why cannot we have the access modifiers for a class other than public and default. If so why? Can't we use the specifiers private or protected. I know we can use these specifiers for the inner classes. But what about the classes other than the inner classes?
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

if a whole class could be private, it would be invisible for the rest of the java universe.

What do you want with such an invisible class?


?
Bu.
 
author and cow tipper
Posts: 5001
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A private class could never be seen or invoked by the compiler, especially if it is a top level class. Innter classes are a bit different.

Same thing goes for interfaces as well.

Cheers!

-Cameron McKenzie
 
Satish Ray
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Thanks for the quick comments.
I agree with the explanation provided so far. But if protected is used as a specifier for the class then what happens in this case?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But if protected is used as a specifier for the class then what happens in this case?

Why don't you just try it out? You'll ofcourse get a compiler error:

In C++, you can have protected and private inheritance, but what it means is not obvious (i.e. inheritance suddenly doesn't mean "is a" anymore) and you don't normally need it. You should be glad that Java doesn't have those obscure C++ features...
[ October 13, 2006: Message edited by: Jesper Young ]
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A private class indeed is fairly useless and only consumes disk space.

A fictive protected class could be useful. It should mean that all public members of the class are accessible by classes in the same package only and by subclasses irrespective of the package. But how much work is it to type "protected void method() {}" instead of "public void method() {}"?

However, the same applies to the abstract modifier. If at least one method is abstract, then you MUST declare the class as abstract (which is meaningless imho). Why doesn't the same apply to protected methods?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic