• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

difference between java acces specifiers?  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need breif difference in private, public, protected, default.
 
Greenhorn
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!

Sincerly,
Your friends at www.javaadvice.com
www.javaadvice.com - The place where your questions are answered directly.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!