Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 access modifier

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In Khalid Mughal Java certification book, it is given that;

"A subclass in another package can only access protected members in the superclass via references of its own type or a subtype."
The example given to eplain this fact is as follows:

package A;

public class SuperclassA
{
protected int SuperclassVarA;
protected void superclassMethodA()
{
}
}


package B;

import A.*;

public class SubclassB extends SuperclassA
{

SuperclassA objRefA = new SubclassB();
SubclassB objRefB = new SubclassB();

void SubclassMethodB()
{
objRefB.superclassMethodA(); //Valid
objRefA.superclassMethodA(); //Invalid

}
}

Can anyone pls explain why objRefA.superclassMethodA(); is invalid.

Rads
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will make an attempt.

protected members are accessible to all classes in same package and all classes that inherit even in other packages. Therefore the protected method of the super class should be avialbale by inheritence. In that sense the method is inside the derived class.

private (and also less restrictive access modifiers) members are accessible to the same class even if from a a different object instance.

objRefB is a reference of the same class. objRefA is not.

This is only an attempt. I hope to hear a better explanation from someone.

I still could not explain the probably related problem


[ June 09, 2004: Message edited by: Swamy Nathan ]
 
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swamy is exactly right. A protected member can only be accessed by a subclass in a different package through inheritance. Protected does not mean that the subclass can treat the protected member as if it were public. This means that if the subclass (that is outside the package) gets a reference to the superclass, a protected member cannot be accessed using dot notiation. In this case, the member may as well be private or default, because that is how it is treated.

Basically, by saying that a subclass has access to a superclass member, we are talking about the subclass inheriting the member, not simply accessing it through a reference to an instance of the superclass (the way any other nonsubclass would access it).

Hope that helped.
 
Swamy Nathan
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also figured out the following regarding the SuperclassA code:-

In fact whether we use a subclass object / reference as long as it is from the same superclass� file it is visible even if the member is private / package with one exception. The only case when it won�t be visible would be if the member were private / package but the object and reference both were of the subclass and the member was not visible in the subclass. In that case even if the code is in the super class file it wont compile which makes quite obvious sense.

All this means the guys who created Java guys like James Gosling were really smart. I can really appreciate the clarity in their creation.
[ June 10, 2004: Message edited by: Swamy Nathan ]
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Corey,

if you had time, could you explain in more detail the question in the first thread, i still confused why objRefA.superclassMethodA(); is considered invalid?

polymorphysm is not working in this case, why?

thanks
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel this code is not working because we are trying to put the reference of SubclassB in a variable of Class SuperclassA. And as has been stated that any protected member of a class can be accessed only from the subclass whether its in the same package or a different package.

Here, when we try to access the protected member through the variable of SuperclassA which is not a subclass of SuperclassA in package B(if you remember the concept of subclass and superclass then it is stated in the books that a class is a subclass of itself also, this is why sometimes they use the term "strict subclasses" for the subclasses other than the class itself.

Yes, SuperclassA is a subclass of SuperclassA in package A but not in package B.

If you try to make such type of reference in package A it will work fine but it will not work in the other packages.


Well, I feel Corey would be a much better person to explain this concept. I have just tried to find an answer and this is the only concept which comes to my mind.

Regards
Kaps
[ June 11, 2004: Message edited by: kapil munjal ]
 
Beny Na
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Kapil..
yes, you right, thanks for your explanation.
To convince my thinking, the key is in the reference(instance variable) should be create from the subclass not from the superclass, in other words polymorphysm is not applied here, right?
correct me if i am wrong, please.


thanks
 
Swamy Nathan
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe things will exlain themselves better if I repeat what I finally understood.

______


The above code is perfectly valid. Forget the details ofSubclass. Just be aware that there is a subclass.In fact whether we use a subclass object / reference as long as it is from the same superclass� file it is visible even if the member is private / package with one exception.

The only case when it won�t be visible would be if the member were private / package but the object and reference both were of the subclass and the member was not visible in the subclass. In that case even if the code is in the super class file it wont compile which makes quite obvious sense.










protected members are accessible to all classes in same package and all classes that inherit even in other packages. Therefore the protected method of the super class should be avialable by inheritence. In that sense the method is inside the derived class.private (and also less restrictive access modifiers) members are accessible to the same class even if from a a different object instance



As far as accessiblity is concerned does it matter whether we are talking of superclassVarA as a field or as amethod. In this discussion its just a member.

I found the example too insightful and fascinating.
Thats why i said the guys who made Java were real geniuses.
I hope they will preserve Goslings brain later for analysis just to see what was going on inside.

Well its Friday here and everyone make sure u have a good weekend with ur special someones.
 
Trust God, but always tether your camel... to this tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic