Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inherited protected member

 
sai donthneni
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
sai donthneni
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sai donthneni
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why not?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic