• Post Reply Bookmark Topic Watch Topic
  • New Topic

Protected variables access  RSS feed

 
Juva Yuva
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

From the link below , i understand that protected variables cannot be inherited if its from different package. Is my understanding right ?

Reference link http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.7,

cudnt exactly get what this means. explanations please ?

"A compile-time error occurs in the method delta here: it cannot access the protected members x and y of its parameter p, because while Point3d (the class in which the references to fields x and y occur) is a subclass of Point (the class in which x and y are declared), it is not involved in the implementation of a Point (the type of the parameter p). The method delta3d can access the protected members of its parameter q, because the class Point3d is a subclass of Point and is involved in the implementation of a Point3d"



http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.7
[ December 19, 2008: Message edited by: Juva Yuva ]
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is explaining something obscure about the inheritence of protected variables. I say "obscure" because most people don't care about it or don't run into this at all (this is, of course, opinion only).

With a subclass, you can access all protected variables of the super class -- not only for the "this" instance, but for any instance passed into it as well. However, the instance that is passed to it, must be at least of the type of the subclass.

This is because, another class can also inherit from the super class and implement it differently. Allowing that access can break that other class type.


As for why the specification clarifies this for sub classes only in different packages... It is because protected variables gives package access. If the protected variable is in the same package, it is allowed access -- as it is allowed access to any protected variable, in any class, in the same package. Hence, the above restriction doesn't apply when it is the same package.

Henry
[ December 19, 2008: Message edited by: Henry Wong ]
 
Juva Yuva
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

With a subclass, you can access all protected variables of the super class -- not only for the "this" instance, but for any instance passed into it as well. However, the instance that is passed to it, must be at least of the type of the subclass.


Thanks Henry. but need more explanation on the visiblity of the protected variables ?
[ December 19, 2008: Message edited by: Juva Yuva ]
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Juva,

If you can get a hold of Cameron's book, its a great help for all Java beginners and is good at explaining the different access modifiers.

Steve
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please give fuller details of the book; many readers won't know which book you mean.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!