• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Members Scope

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please put some comment whether it is correct or not ...

thanks .
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
If you're gonna buy things, buy this thing and I get a fat kickback:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic