• 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 variables access

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please give fuller details of the book; many readers won't know which book you mean.
 
reply
    Bookmark Topic Watch Topic
  • New Topic