• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

inherited protected member

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why not?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic