• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

protected member

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not a threads topic.... moving to Java in General, Beginner.

Henry
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any replies?
Regards
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic