• 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 ...
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

difference between java acces specifiers?  RSS feed

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need breif difference in private, public, protected, default.
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The access modifiers in Java are 'public, private, protected, or package-private (no modifier)'

In Java there are two levels of access control.

1. The class level or top level (public or package-private access)
2. The memeber level (public, private, protected, or package-private)

The class level:

When a class is declared 'public', it is visible across packages. But when it is defined package-private or default (that is without any modifier), the class is visible only to other classes within the same package.

public class JavaAdvice -> visible across packages.
class JavaAdvice -> visible only within the package.

At the class level only public or default access are allowed


The memeber level:

The following table shows the access to members permitted by each modifier.

Access Levels:

Modifier Class Package Subclass World
-------- ----- ------- ------- ------
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N

In Summary,
A class can be defined public or default (no modifier) and class members are allowed to have one of 'public, private, protected, or package-private (no modifier)'. Public members are visible across packages. Any private member of a class is not visible outside of the class. Any protected members are available to the class and its subclass. Default members are visible in the class and within the same package.

Hope this helps!

Your friends at www.javaadvice.com
www.javaadvice.com - The place where your questions are answered directly.
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!