Why is a protected member of a class visible to a subclass of the class that declares it outside the package in which that member is declared, but not visible to an object of the class that declares it in another package? The answer because the language specification dictates so is not what I'm looking for. I ask for logic.
package clase1;
public class Clase1 {
protected String nombre;
protected String getNombre(){
return nombre;
}
}
---------------------------------------------------
package otropaquete;
import clase1.Clase1;
public class Subclase extends Clase1 {
public void accediendoClase1(){
Clase1 obj=new Clase1();
Subclase obj2=new Subclase();
Clase1 obj3=new Subclase();
obj2.getNombre();// esto funciona
//obj.getNombre();-->Esto no funciona
//ob3.getNombre();-->Esto no funciona
}
}
It seems to me that you are confusing yourself when you say but it is not visible by an object of the class that declares it in another package .
It is not that it is not visible to an object of the class, it is that the client class cannot see the private or protected attributes of the class used.
Let me explain: when we define a parent class (there will be child classes that inherit from it), as shown below:
The field
atributo
is visible within the code of the class itself. It is logical, because an object of the classPadre
needs to make use of its attributes to be able to change the state of the program and advance in the execution.Now, the object that makes use of its field
atributo
does so through the methods defined in the class itself. That is, the code that handles, modifies or reads from that field will be executed by the object in question.Protected visibility allows that field (method or attribute) to be visible from any class that inherits from class
Padre
. For example:However, when we use the parent class inside a class, we say that it is a client
Padre
because it uses it.When a value is assigned to the attribute of the called object
padre
, that piece of code does not mean that said object is doing it, on the contrary, it is the client that is changing the value of the object's field , which it cannot do, because the classCliente
does not have access to the fieldatributo
.Therefore, seeing a dot does not mean that the object does something. Sometimes yes and sometimes no.
When a method of the type is executed,
padre.setAtributo()
we are asking the object to do something, so it is the object that performs the action. Whereas when we access a public attribute withpadre.atributo
the object it does nothing, it is the client that performs the action.private restricts access to the class itself regardless of the package.
protected to the class and the classes that inherit from it regardless of the package.
undefined , or package-level access restriction, restricts access to classes in the package where the method or class with that access restriction is located.
public access is not restricted, i.e. it can be accessed by making a static reference or by having an object.
In your code what happens is; when the object of Class1 or the one created as "fake child of Class1" calls the method it seeks to execute it from its code in Class1 , since the code of said class is in another package it cannot access it and therefore it is not visible, on the other hand , when the Subclass object calls the method, it seeks to execute it from its code, as Subclass inherits from Class1 , it has resources in its own code by inheritance to execute it from there without referring to the code of Class1 , then it can access and is visible.