File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Members Scope 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 "Members Scope" Watch "Members Scope" New topic
Author

Members Scope

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Hi all ,

I don't know exactly what will be output of this mail ...specially table ..
But this table is very good for SCJP aspirants like me ...

How to look :

First of all there are two methods of accessing members of a class by other class :
1] Subclass(Inheritance) -> In this , the other class is the subclass of a class which has members .

class A{
int i;
}

class B extends A{
public static void main(String[] s) {
System.out.println(i);
}
}

2] With same class reference -> In this , the other class is not a subclass of a class which has members . We are accessing members by the class reference which has members .

class C{
public static void main(String[] s) {
B b = new B();
System.out.println(b.i);
}
}


Now there are 4 types of members a class may have :
private, public, protected, default .

in pac -> in the same package .
out pac -> in other package .


Members in one class----Subclass(Inheritance)-----With same class reference


private-----------------no-----------------------no

public------------------yes----------------------yes

protected---------------in pac-------out pac------in pac--------out pac
------------------------yes---------yes---------yes----------no

default
------------------------yes---------no----------yes----------no


Any body please check & mention that the table is correct or not ...

thanks .
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
please put some comment whether it is correct or not ...

thanks .
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I think that protected access may need some elaboration, especially since Kathy said that the details of access control were exam bait.

The simple case is protected access to a static member x of class A in package P. Code in class A, code in any other class in package P, and code in any subclass S of class A can access member x in all the normal ways.

The interesting case is where x is an instance member of class A in package P. Code in class A and code in any other class in package P can still access member x normally. Code that is within a subclass S of A but that is not in package P can only access member x using a reference of subclass S or of a subclass of S. this, explicit or implied, is considered a reference of subclass S. super is not.

The key point is that references of any supertype of S, including A, will not work.

Since an interface can never be a subtype of a class, that would mean that you can't access a protected member from outside its package using an interface name.

There is a very good exposition of this topic in K&B pages 75-81.


Mike Gershman
SCJP 1.4, SCWCD in process
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Members Scope