aspose file tools*
The moose likes Beginning Java and the fly likes subclass outside package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "subclass outside package " Watch "subclass outside package " New topic
Author

subclass outside package

sai donthneni
Ranch Hand

Joined: Mar 26, 2006
Posts: 50
When a subclass outside the package inherits a protected member, the member is essentially private inside the subclass, such that only subclass and its subclasses can access it........ but the private members are not accessed by the subclasses of the class, then how is the first statment right? Am i clear or does it look ambiguous?
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Could you pls specify it some more so that we can get to know what you really want to ask.??


The Best way to predict your future is to create it - Every great individual common man
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Thank you.
You are using the word "private" to mean one thing in the first half of the sentence, then something different and more specific in the second half.
Private access means that member can only be accessed from inside its own class. [That does include inner classes.] So far, so good?

If you miss out the access modifier keyword, you get default access: access to members from within their own class, and any other classes in the same package. But you don't get access from subclasses outside the package. Am I still making sense?

People who have only programmed in Java have no difficulty with "protected," but people who have programmed for a long time and have used C can get confused, because the protected keyword in C means something different.
What it means in Java is that access is available from within the same package, and from subclasses, in the same package and in different packages.
So, if you have a class foo in package "B" which inherits a protected member from a superclass bar in package "A," then access to that member in package "B" is confined to the class foo which inherits it.

I think that is what you mean, and if I have interpreted what you meant to say, you are correct.
:-)
CR
sai donthneni
Ranch Hand

Joined: Mar 26, 2006
Posts: 50
What I am trying to say is that if there is class A in package 'a' and it has a protected member variable.

package a;
class A
{
protected int a;
}

I have one more class B which is in a differnt package called b and it extends class A in package a. The proected member of class A, does it become private member to class B when it is extended or it remains protected?

Now can a class C in package b can access it without subcalling it?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Sorry it has taken me so long to reply.

Try it. Far better to run a set of little classes with those access modifiers in; your compiler will give you the answer. That is the correct response.

The incorrect response is, "No."

If your member in the subclass B in package b is inherited from class A in packaage a and has protected access, then the only way you can get access to it from other classes in package b which don't extend class A is to have accessor (get) and mutator (set) methods in class A or B which class C can gain access to. If they are in class A, they would have to have public access, if in B, then any access other than private would work.

CR
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: subclass outside package