Gaucho,
 
    O modificador protected funciona EXATAMENTE como deveria, est� na especificacao da linguagem Java. Se n�o acreditar olhe os seguintes docs (escrito pelos criadores do Java) :
 
    http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#36191
e
    http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#104285
 
    [Em vermelho, a parte que define isso]

6.6.1 Determining Accessibility

  • A package is always accessible.
  • If a class or interface type is declared public, then it may be accessed by any code, provided that the compilation unit (�7.3) in which it is declared is observable. If a top level class or interface type is not declared public, then it may be accessed only from within the package in which it is declared.
  • An array type is accessible if and only if its element type is accessible.
  • A member (class, interface, field, or method) of a reference (class, interface, or array) type or a constructor of a class type is accessible only if the type is accessible and the member or constructor is declared to permit access:
    • If the member or constructor is declared public, then access is permitted. All members of interfaces are implicitly public.
    • Otherwise, if the member or constructor is declared protected, then access is permitted only when one of the following is true:
        • Access to the member or constructor occurs from within the package containing the class in which the protected member or constructor is declared.
        • Access is correct as described in �6.6.2.
    • Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (�7.6) that encloses the declaration of the member.
    • Otherwise, we say there is default access, which is permitted only when the access occurs from within the package in which the type is declared.
 
 
 
----- Original Message -----
From: "Gaucho" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, April 11, 2002 8:19 AM
Subject: [java-list] bug Java - protected

> Algu�m sabe se no jdk1.4 o problema do protected j� foi resolvido ?
>
> Para quem n�o sabe, o modificador protected n�o funciona como deveria.
>
> Na verdade, em Java, quando voc� declara uma vari�vel protegida, ela acaba
> tendo acessibilidade p�blica.
>
> O �nico encapsulamento provido pelo protected � o encapsulamento em rela��o
> ao pacotes. Mas no caso da implementa��o de classes desvinculadas de
> pacotes, o modificador "protected" infelizmente n�o funciona...
>
> Duvida? :^)) fa�a um teste: implemente uma classe com algum membro
> "protected" e depois tente acess�-lo de outra classe que n�o seja estendida
> da primeira:
>
> class Superclasse
> {
> protected String nome = null;
> }
>
> public class Teste
> {
> static public void main(String[] args)
> {
> new Teste();
> }
>
> Teste()
> {
> Superclasse pai = new Superclasse();
>
> // isso n�o deveria ser acess�vel somente pela classe
> // Superclasse ou uma de suas subclasses ??
> pai.nome = "oo";
> }
> }
>
>
> ------------------------------ LISTA SOUJAVA ----------------------------
>
http://www.soujava.org.br  -  Sociedade de Usu�rios Java da Sucesu-SP
> d�vidas mais comuns:
http://www.soujava.org.br/faq.htm
> regras da lista: http://www.soujava.org.br/regras.htm
> historico: http://www.mail-archive.com/java-list%40soujava.org.br
> para sair da lista: envie email para [EMAIL PROTECTED]
> -------------------------------------------------------------------------
>

Responder a