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