Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • 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: 5006
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?
 
The only cure for that is hours of television radiation. And this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic