Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Accessibility Modifiers for classes

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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
  • 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: 5000
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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?
 
Seriously Rick? Seriously? You might as well just read this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!