• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

protected accessibility

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
i have having problem in understanding protected accesibitlity
SuperclassA contains a protected member varA in the default package(ie package name given)
the subclassB is in another package and extends SuperclassA
still it can't access varA using a reference of SuperclassA.


why is varA accesible in SubclassB through obj3 and not through obj1
and obj2.
the compiler gives error tht varA has protected access in SuperclassA.
madhur.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you specify the error message?
would you also paste the code from other package?
[ September 22, 2002: Message edited by: Barkat Mardhani ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a perfect example to understand the protected access.
I guess varA is declared with protected in base classs.
The compile type of the variable used in the access expression for the protected variable must be the same as the type of class in which the access expression for the protected member occurs. The same or one of its subclasses. Let's see the examples:
The type of the class in which the access expressions for the protected varA occurs is SuclassB.
i=obj3.varA; //OK
The compile type of obj3 is SubclassB, thus OK
i=obj1.varA; ERROR
Because the compile type of obj1 is SubclassA
i=obj2.varA; ERROR
Because the compile type of obj2 is SubclassA
In the
JLS 6.6.2 section you can read a more formal explanation.
[ September 22, 2002: Message edited by: Jose Botella ]
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Madhur:
I will try to explain it in different way. Though Jose's explaination is perfect.
When you are saying in subclassB:
int i = varA;
You are saying that assign the value of varA to i. You are mentioning varA as if it is another instance variable in subclassB. When compiler does not find it in subclassB, it tries to look for it higher up in hierarchy. It finds it in superclassA and it is protected. So it is available in subclassB. And value get assigned.
However, think for a second that hierarch is of 5 levels and protected i is reassigned in each level from 1 to 5 so that lowest in hierarchy has i defined as 5. Now if your subclassB was sub class of this lowest level class, the i in subclassB will get 5 automatically by saying:
int i = varA; // i get 5
because that is immediately avaiable to subclassB.
Now let us say that in subclassB you want i to get value from second top class in hierarchy. So you are creating an object of that class (call it class2) by saying:
class2 obj2 = new class2();
to assign the value to i, you are saying:
i = obj2.i;
remember that i is defined in subclassB so it class level is subclassB. While obj2.i is member of class2, so its class level is class2. You can not take a member of higher class level and put it's value in lower class level member.
So in nutshell, the protected members are available in a derived class from immediate super class. You can not get a value of specific super class.
Hope this helps.
Barkat
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also:
int i = varA;
Within an instance method, constructor, variable or block instance initializer is the same as
int i = this.varA;
The compile time of this is the inmediate enclosing class, SubclassB, thus it's OK
[ September 23, 2002: Message edited by: Jose Botella ]
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm quoting the following from Mughal book


A subclass in a nother package can only access protected members in the superclass via references of ITS own type or a subtype.
....

 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JLS example to make things clearer (???)
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic