Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Protected Memebres Cannot Acces In Sub class...

 
Thangaraj Selvamani
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why it shows compile error eventhough "i" variable declared has a protected member...
according to my knowledge
protected members can be inherited from the super class to base class eventhough both class in different pckages.
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this thread.
 
subhasish nag
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'st ,you need to import p1.A to directly access the class.
And the reason actualy needed by you is "protected members can only be accessible by sub classes from diffrent package only through inheritance.But they can be accesible in the clild class if you accesss it by creating child class instance and access through it(through inheritance).But it can't be accesible by parent class instance in child class"
 
Jolly Tiwari
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! subhashish,

This is true that you can access a protected data member through inheritence across packages but i will like to emphasize on the fact that you can't access it even with the reference referring the sub class instance.
as that member is treated like a private member within the sub class.

i.e ref.<some inherited data member > is not allowed from some third class having reference of sub class.



Regards,
Jolly
 
subhasish nag
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jolly for this point.Here I am only trying to mention the access within the Sub class ,but might have forgotten to mention .
[ October 16, 2008: Message edited by: subhasish nag ]
 
Thangaraj Selvamani
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if suppose both class in same file...
it is not showing compile error...Why?

EG:
class A {
protected int i = 10;
public int getI() { return i; }
}

public class B extends A {
public void process(A a)
{ a.i = a.i*2; }
public static void main(String[] args)
{
A a = new B();
B b = new B();
b.process(a);
System.out.println( b.i);
}
}
 
Jolly Tiwari
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Within a package , a protected data member will behave very much like a data member with default access and is accessible using normal convention of .(dot) operator.

Its beauty come into picture when we are giving controlled acccess to some data member across the package.


Regards

Jolly
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also remember here that you can access protected static members of super class in sub-classes in different packages.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic