File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes inherited protected member Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inherited protected member" Watch "inherited protected member" New topic
Author

inherited protected member

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?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


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?

No. Your protected variable is visible to all classes that extend the class it was defined in.



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

No. Protected variables are only visible to classes that extend the class where they are defined and any class in the same package.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
sai donthneni
Ranch Hand

Joined: Mar 26, 2006
Posts: 50
what I am asking is, can i access the proected member in class C in package b without extending class B?
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by sai donthneni:
what I am asking is, can i access the proected member in class C in package b without extending class B?


No.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
sai donthneni
Ranch Hand

Joined: Mar 26, 2006
Posts: 50
why not?
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by Paul Sturrock:

Protected variables are only visible to classes that extend the class where they are defined and any class in the same package.
sai donthneni
Ranch Hand

Joined: Mar 26, 2006
Posts: 50
what I am asking is, can i access the proected member in class C in package b without extending class B?

what if i make class C extend class B? Will i be able access the protected member of class A which inturn is extended by class B?
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by sai donthneni:
what I am asking is, can i access the proected member in class C in package b without extending class B?


No.

A protected member of class A is only visible to classes in the same package as A or in the body of a subclass of A. If class C is not in the same package as class A and is not a subclass of class A then it does not have access to any protected member of class A.

Originally posted by sai donthneni:
what if i make class C extend class B? Will i be able access the protected member of class A which inturn is extended by class B?


Yes. In this case class C would be a subclass of class A and therefore have access to any protected member of class A.
[ April 12, 2006: Message edited by: Ken Blair ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
what I am asking is, can i access the proected member in class C in package b without extending class B?

No, only the class which actually extends A can access its protected content.


what if i make class C extend class B? Will i be able access the protected member of class A which inturn is extended by class B?

Yes if C extends B and B extends A then C can access protected content in A.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inherited protected member