wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes protected Vs default access modifiers? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "protected Vs default access modifiers?" Watch "protected Vs default access modifiers?" New topic

protected Vs default access modifiers?

Mallik Avula
Ranch Hand

Joined: Nov 30, 2006
Posts: 86
hi friends
what is the exact difference between protected and default access modifiers?
as i know that "protected" can be accessible only from subclasses regardless of whether that class is in same package or not
and "default" can be accessible from any class in the same package regardless of whether that is extended or not.

but my code:

class Base{

protected void method2(){
System.out.println("base thothd2");

public class Test {

public static void main(String[] args){

Base t = new Base();



output is:
Base method2

so, that means a class can access protected members without extending if that class in same package.
is it write?(still i cann't believe)
give me clarification whether it is write or any other involved.....


Thanks & Regards<br />Mallik Avula<br />SCJP1.4
Mallik Avula
Ranch Hand

Joined: Nov 30, 2006
Posts: 86
hi friends
i am very sorry for small mistake
is it right? not(is it write)
i am really very sorry

Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Access Specifier scope
"public" scope protected access + any other outer pack classes
"protected" scope default pack access + any sub class relation
"default" scope (means absence of access specifier)-- classes in same pack
"private" - methods in same class in which its defined.

Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Gaurav Pavan Kumar Jain
Ranch Hand

Joined: Mar 19, 2007
Posts: 168
Hi Mallik

protected provides you access in following conditions:-

1) From Same class
2) From any class from same package
3) from a subclass from same package
4) From a Subclass outside the package

I agree. Here's the link: http://aspose.com/file-tools
subject: protected Vs default access modifiers?
It's not a secret anymore!