| Author |
protected member
|
vini singh
Greenhorn
Joined: Dec 04, 2008
Posts: 18
|
|
package p; class superc { protected int x=6; } package n; import p.*; class subc extends superc {public static void main(String s[]) { superc c=new superc(); System.out.println(c.x); } } its showing compiler error as protected variable cant be accessed through the parent reference in subclass. plzzz xplain
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16684
|
|
Not a threads topic.... moving to Java in General, Beginner. Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24050
|
|
Originally posted by vini singh: its showing compiler error as protected variable cant be accessed through the parent reference in subclass.
A subclass can access the superclass's protected members only through a reference of the subclass's type. In other words if, in main(), you said subc c=new subc(); System.out.println(c.x); that would work. The reason for this rule is simple. Imagine that parent class P has subclasses C1 and C2. There's no reason to think that the classes C1 and C2 have any special security relationship. But without this rule, an instance of C1 would have access to some otherwise-private variables in instances of C2.
|
[Jess in Action][AskingGoodQuestions]
|
 |
ramya narayanan
Ranch Hand
Joined: Oct 06, 2008
Posts: 338
|
|
Dear Hill, I need a small clarification from you. I accept the rule that
A subclass can access the superclass's protected members only through a reference of the subclass's type
But the explanation you gave for that rule is where I got struck up.
The reason for this rule is simple. Imagine that parent class P has subclasses C1 and C2. There's no reason to think that the classes C1 and C2 have any special security relationship. But without this rule, an instance of C1 would have access to some otherwise-private variables in instances of C2.
As far as I've understood you say that there are two subclasses C1 & C2 both of which inherit class P
class C1 extends P { } class C2 extends P { private int c2variable=12; }
As you said classes C1 & C2 are not related in any way except both of them inherit the same parent class P. In this case how without the quoted rule, QUOTE] A subclass can access the superclass's protected members only through a reference of the subclass's type
an instance of c1 would have access to some otherwise-private variables in instances of C2. Are you saying this: 1)Consider a class C2 has protected variable which we're trying to modify in a class TestC2 by delegating the C2 instance to it. Note when I compile it in the same package no error is surfacing
class C2 { protected int c2variable=12; } public class TestC2 { C2 c2instance=null; public int createInstance(C2 c2instance) { this.c2instance=c2instance; this.c2instance.c2variable=20; return this.c2instance.c2variable; } public static void main(String[] args) { TestC2 test=new TestC2(); C2 c2=new C2(); System.out.println("Original c2 var value is "+c2.c2variable); System.out.println("Value :"+test.createInstance(c2)); } }
The output is:
C:\sai> java TestC2 Original c2 var value is 12 Value :20
2) But when the c2variable in C2 is made as private I'm not able to access it. The compiler error is:
C:\sai>javac TestC2.java TestC2.java:8: c2variable has private access in C2 this.c2instance.c2variable=20; ^ TestC2.java:9: c2variable has private access in C2 return this.c2instance.c2variable; ^ TestC2.java:16: c2variable has private access in C2 System.out.println("Original c2 var value is "+c2.c2variable); ^ 3 errors
3) When I extend the C2 class ( Which now has a private variable) in TestC3, the subclass is not able to access the private variable.
public class TestC3 extends C2 { public static void main(String[] args) { TestC3 test=new Testc3(); test.modifyVariable(); } public void modifyVariable() { super.c2variable=20; System.out.println("In method c2variable modified to :"+super.c2variable); } }
The compiler error is :
C:\sai>javac TestC3.java TestC3.java:5: cannot find symbol symbol : class Testc3 location: class TestC3 TestC3 test=new Testc3(); ^ TestC3.java:11: c2variable has private access in C2 super.c2variable=20; ^ TestC3.java:12: c2variable has private access in C2 System.out.println("In method c2variable modified to :"+super.c2variable); ^ 3 errors
If you can brief about your explanation for that rule, it would be better for me. Regards
|
 |
ramya narayanan
Ranch Hand
Joined: Oct 06, 2008
Posts: 338
|
|
Any replies? Regards
|
 |
ramya narayanan
Ranch Hand
Joined: Oct 06, 2008
Posts: 338
|
|
But when we try to access the superclass protected variable inside the same package in a subclass through a superclass reference, it's working
package n; class superc { protected int x=6; }
package n; class subc extends superc { public static void main(String s[]) { superc c=new superc(); System.out.println(c.x); } }
Compile & run it
C:\sai>javac -d . subc.java C:\sai>java n/subc 6
When we can access the protected property inside the same package through a superclass reference, then why have they enforced the restrictions on accessing the protected property in other packages. Regards.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32644
|
|
|
The "protected" modifier allows access in subclasses, as well as "default" access. And "default" access allows access in the same package, so "protected" includes the same package.
|
 |
ramya narayanan
Ranch Hand
Joined: Oct 06, 2008
Posts: 338
|
|
When we can access the protected property inside the same package through a superclass reference, then why have they enforced the restrictions on accessing the protected property in other packages. Regards.
I want to know why they have enforced this condition. What is the design reason behind this? Regards.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32644
|
|
You have to make a decision; there are different granularities of access. In Eiffel you can give access to a list of classes, in C++ to a list of classes or functions, in Java there is less granularity. But allowing access from other packages would be little different from public. You have to make a decision; how would you organise access?
|
 |
Ivan Koblik
Greenhorn
Joined: Apr 22, 2008
Posts: 8
|
|
I think it is presumed that classes within a package are usually written by the same developer and consequently it is safe to give her/him more access rights, like the default access to all the protected class members. Other packages, on the contrary, may be written by some other developers, so here we see more constraints being imposed.
|
 |
 |
|
|
subject: protected member
|
|
|