Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

regarding protected scope

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The above program compile perfectly

but if i wrote the class subtest in a different package


While comiling subTest its showing

d:\javatest\package\subTest.java:8: a has protected access in pack1.Test
System.out.println(r.a);
^
1 error

I'm confussed....with it....if its in protected access why it was showing it in the first instance..
[ August 03, 2007: Message edited by: yogesh srinivasan ]
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi yogesh!
Use "code" button whenever you are posting sample source-code.


The above program compile perfectly



Are you sure the first one really did?
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
protected members can't be accessed outside package using dot operator with reference variable of parent class except in the same package.So in your code no need to use reference variable to access protected variable outside package,you must access protected variable with using dot operator outside the package, using subclass reference variable,So that your code compiles fine!
[ August 03, 2007: Message edited by: srinivas marasu ]
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


protected members can't be accessed outside package using dot operator


Are you sure protected members can't be accessed outside package using dot operator?
 
yogesh srinivasan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have checked it in my system and have done compleing using jdk1.4.2
and thats wat i have observered

Please reply to my question and dont comment unnecessarily
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yogesh! I would like to know the filename of the code you have saved it in and output that you are getting. And it is obvious on your side to get frustrated, but I only want you to ask questions that are directed in right direction. Dont worry buddy! i will be here and I will be here until I see you getting sun certified. [ ]
 
srinivas marasu
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K&B book 1st chapter page no:-34

what does it mean for a subclass-outside-the-package to have access to a
superclass (parent) member? It means the subclass inherits the member. It does not,
however, mean the subclass-outside-the-package can access the member using a
reference to an instance of the superclass. In other words, protected = inheritance.
Protected does not mean that the subclass can treat the protected superclass member
as though it were public. So if the subclass-outside-the-package gets a reference to
the superclass (by, for example, creating an instance of the superclass somewhere
in the subclass' code), the subclass cannot use the dot operator on the superclass
reference to access the protected member. To a subclass-outside-the-package, a
protected member might as well be default (or even private), when the subclass is
using a reference to the superclass. The subclass can see the protected member
only through inheritance
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by srinivas marasu:
K&B book 1st chapter page no:-34

what does it mean for a subclass-outside-the-package to have access to a
superclass (parent) member? It means the subclass inherits the member. It does not, however, mean the subclass-outside-the-package can access the member using a reference to an instance of the superclass. In other words, protected = inheritance. Protected does not mean that the subclass can treat the protected superclass member as though it were public. So if the subclass-outside-the-package gets a reference to the superclass (by, for example, creating an instance of the superclass somewhere in the subclass' code), the subclass cannot use the dot operator ]on the superclass
reference to access the protected member. To a subclass-outside-the-package, a protected member might as well be default (or even private), when the subclass is using a reference to the superclass. The subclass can see the protected member only through inheritance



How about a sub-class in different package using the protected member of the parent class in another package with subclass's own reference and subclass's own object ?

Srini! it is not the dot operator, it is the dot operator with parent reference outside the package.
 
srinivas marasu
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Akhilesh!
Yes obviously,subclass in a different package can access protected members using it's own reference,using dot operator,As I mentioned in context of above code,you must not use dot operator to access protected member using super class reference,to support this statement i posted from K&B book.
Once again i want to confirm you that in the context of above code.which is using superclass reference to access protected member using dot operator in a different package.
[ August 03, 2007: Message edited by: srinivas marasu ]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following ran just fine and I believe it answers some of your questions.

Spending some time playing with the possibilities is a good way to learn (at least I hope so).
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Jim ,i have some doubt on the above code....
please can you explain the following...


There are three methods as below...



in the first case you have used super keyword,
as per i know super is used to call constructor
of super class,how this possible
that we are accessing protected variables?


in the second case you have used this keyword..
As per i know this keyword refers
to current instance of class,as here current class is subTest
so instance variables of class subTest must be
called so how you can access protected members
of class Test?


Last case you have accessed directly protected variable
a.....how this is possible since both the
classes are in different packages atleast we need
a instance of subTest class to access variable a isnt it?


After seeing these concepts of
protected access i am totally confused please
explain .........
 
yogesh srinivasan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry Akhilesh Trivedi I was a little in the morning........ Just misslead with inheritance and . operator,, But still If you are able to see a protected varaiable any where inside a package using .operator then wats the use of it........
[ August 03, 2007: Message edited by: yogesh srinivasan ]
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Inheritance is IS-A Model and Delegation is HAS-A Model.

During inheritance no matter what your sub class package is, all the protected members are part of sub class.

When using Delegation(I mean using reference) - protected means default.protected dosent make sense for delegation.

So in the above case your using Super class reference to access its variable(Delegation) on a var that is having default access (Though it is protected- remember it is delegation).Instead if you used the sub class reference it works.
 
Jim Knighten
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dhwani, sorry to be late on this. Check my comments to your comments. But first, have you tried running the code I posted? That is a good way to see how things work and don't work. Notice that all three dispx() methods use Test.a via inheritance. As long as subTest doesn't hide Test.a with it's own instance member "a" all three dispx() methods are accessing the same variable. All protected members of a superclass are visible to any subClasses whether or not the subClasses are in the same package or not.





in the first case you have used super keyword,
as per i know super is used to call constructor
of super class,how this possible
that we are accessing protected variables?

JK - In this case we are accessing super via inheritance. Super is always available to a subclass after the super constructor has been run. It is a special reference to the core essense/object of the subclass instance. Try setting super to a new instance of Test() and see what happens.

in the second case you have used this keyword..
As per i know this keyword refers
to current instance of class,as here current class is subTest
so instance variables of class subTest must be
called so how you can access protected members
of class Test?

JK - the current instance of subTest "inherits" any protected members of it's super class as if they were it's own members. There is no difference between it's own members and those it inherits from it's superclass. Unless of course subTest creates it's own member named "a". Try adding member int a = 15; to subTest. In this case subTest.a hides super.a.

Last case you have accessed directly protected variable
a.....how this is possible since both the
classes are in different packages atleast we need
a instance of subTest class to access variable a isnt it?


JK - Keep in mind that we are accessing and printing out the value of "a" within an instance of subTest via inheritance. The code is not accessing "a" from outside of subTest. Try creating another class in pack2 that tries to access mySubTestInstance.a.


After seeing these concepts of
protected access i am totally confused please
explain ......... [/QB]


JK - I sympathise with you. It seems like Sun overloaded the "protected" keywork. They should have created two access keywords. "packageprotected" and "inheritable".
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic