probelm with protected members of the class....

A

arya

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 ?
 
E

Eric Sosman

arya said:
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 ?

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.
 
J

Jason Cavett

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 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.
 
L

Lew

Jason said:
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
 
A

arya

Actually it defaulted to default access, not private.

-- 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?
 
T

Tom Hawtin

arya said:
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
 
L

Lew

Tom said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top