Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

probelm with protected members of the class....

Thread view: 
arya - 07 Mar 2007 12:29 GMT
hi guys, I am facing problem with accessing protected members of
parent(base)class in child (derived) class.It's give me compile time
error (Filed s is not visible )when i am trying to access field s in
parent class from within child class.

My code is as follows :

(1) Parent Class :

package com.parent;

class My{
    String s = "My";
}

(2)Child Class :

package com.child

import com.parent.My

public class Test extends My{

    protected String s = "Test";

    public static void main(String[] args){
        My my = new My();
        System.out.println(my.s);// give me compile time error here
specifying that "field"
    }                                           // is not visible
}

please help me.... Can't I access protected members of the class from
within subclass in different package ?
Eric Sosman - 07 Mar 2007 13:18 GMT
> hi guys, I am facing problem with accessing protected members of
> parent(base)class in child (derived) class.It's give me compile time
[quoted text clipped - 30 lines]
> please help me.... Can't I access protected members of the class from
> within subclass in different package ?

    The member `s' in class My is not `protected' at all;
it is package-private.  If you want it to be `protected',
you need to say so when you declare it.

Signature

Eric Sosman
esosman@acm-dot-org.invalid

Jason Cavett - 07 Mar 2007 13:48 GMT
> hi guys, I am facing problem with accessing protected members of
> parent(base)class in child (derived) class.It's give me compile time
[quoted text clipped - 32 lines]
> please help me.... Can't I access protected members of the class from
> within subclass in different package ?

Eric gave you the right answer, but to clarify a bit...

When you didn't declare "String s" as private, protected or public in
your parent class, it defaulted to private.  Make sure "s" is declared
as protected in the PARENT class.  Then, when you create your child
class, you don't need to create that protected "s" variable.  Instead,
all you need to do is...

public class Test extends My{
 public static void main(String[] args){
   My my = new My();
   System.out.println(my.s);
 }
}

Because "s" is now protected in the parent, it can be accessed this
way.  However, this really doesn't make too much sense (in terms of
use).  Instead, by extending "My" your Test class has its own "s"
variable (because Test is a My).  Try it...

public class Test extends My {
 public static void main(String[] args) {
   this.s = new String("Test has s");
   System.out.println(this.s);
 }
}

I'd recommend looking for inforamtion on Google for the differences
(especially with scope) in terms of public/protected/private modifiers.
Lew - 07 Mar 2007 14:19 GMT
> Eric gave you the right answer, but to clarify a bit...
>
> When you didn't declare "String s" as private, protected or public in
> your parent class, it defaulted to private.  

Actually it defaulted to default access, not private.

-- Lew
arya - 08 Mar 2007 05:44 GMT
> > Eric gave you the right answer, but to clarify a bit...
>
[quoted text clipped - 4 lines]
>
> -- Lew

Thanks you guys for your replies. Sorry to say that i forgot to write
'protected' in front of s in parent class. My s is proteted in parent
class. and yesterday i come to know that i can only access protected
members in different package with subclass reference. I can not access
them with parent reference. Can anyone tell me why is this so?
Oliver Wong - 13 Mar 2007 21:58 GMT
> and yesterday i come to know that i can only access protected
> members in different package with subclass reference. I can not access
> them with parent reference. Can anyone tell me why is this so?

   I don't know what you mean by "subclass reference" and "parent
reference", but hopefully this will clarify things for you:
http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

   - Oliver
Tom Hawtin - 13 Mar 2007 22:27 GMT
>            yesterday i come to know that i can only access protected
> members in different package with subclass reference. I can not access
> them with parent reference. Can anyone tell me why is this so?

You seem to be trying to use protected as if meant the same thing as
public. It means almost the same, but does have some minor uses.

Suppose classes X and Y both extend class Base, which has a protected
instance member p.

package b;
abstract class Base { protected int p; }

package x;
class X extends b.Base { ... }

package y;
class Y extends b.Base { ... }

The idea is that X can only access p of instances of type X. X should
not be able to access p in an instance of Y.

If a variable is declared to have type Base, then it's not explicit
which implementation class it is an instance of. Therefore, code in X
and Y should not be able to access its protected members.

Whenever you feel the need to declare something as protected, think a
bit harder about it until you change your mind. There are some instances
where it is genuinely useful, but they are quite rare.

Tom Hawtin
Lew - 14 Mar 2007 02:42 GMT
>  to access its protected members.

Not only was this one of the clearest yet brief elucidations of a concept I
have read, but it used the correct "its" spelling.

> Whenever you feel the need to declare something as protected, think a
> bit harder about it until you change your mind. There are some instances
> where it is genuinely useful, but they are quite rare.

Maybe not "rare" as in "infrequent" so much as in "for very specific and
tightly constrained purposes". When used correctly, protected access is powerful.

I have used protected connection parameters in data access objects. I suppose
there was a better way, now that I am thinking about Tom's advice, but it was
convenient. Perhaps protected accessor methods instead.

More often than members, I declare protected methods for certain subclass
functionality from outside the base class package. Such methods usually occur
only in abstract classes, and usually are final. One example is a data access
object connection establishment method.

Statistically frequent or not, these are particular situations with specific
reasons to use protected access. Protected access is a tool for the design of
heritable classes, also something to do sparingly.

That fits in the meme set of "quite rare".

- Lew


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.